How I set smaller names in variables in Dim section?

  1. I would like to make in Dim declariation “com.sun.star.table.BorderLine2” shorter.
  2. However, it is not declared in the right fashion if I use before:
  3. oStarTable = com.sun.star.table
  4. or even afterwards, removing “as new”
  • So, how I can do this ?
  • What Dim means?
  • What “as new” is doing?
Dim oLine             As New com.sun.star.table.BorderLine2
oCursor     = ThisComponent.CurrentController.ViewCursor	
       
' oStarTable        = com.sun.star.table
' oLine             = oStarTable.BorderLine2

With oLine
    .Color = 0
    .InnerLineWidth = 10
    .LineDistance = 60 
    .LineStyle = oStarTable.BorderLineStyle.DOUBLE 
    .LineWidth = 5
    .OuterLineWidth = 10
End With

If you apply the construct ‘Dim obj As New SomeObject’, then an instance of the object will be created automatically the first time you access it in the code. ‘Dim obj As SomeObject’ will require an explicit assignment of an instance of an object to a variable: obj = New SomeObject
This allows you to control when the object is created.

So it is possible: e.g.
Dim oCellContentType As Object
oCellContentType = com.sun.star.table.CellContentType
If oCell.Type = oCellContentType.TEXT Then '.TEXT=2

You can assign any part of the ‘path’ to a variable. You have to get used to the cumbersome paths

1 Like

In Python you can create a new UNO structure instance like this.

from com.sun.star.table import BorderLine
borderLine = BorderLine()

In contrast, the following Basic code gives an Invalid Object Reference error.

BorderLine = com.sun.star.table.BorderLine
Dim oLine As New BorderLine

So my recommendation is to use Python and all your problems can be solved. :slight_smile:

Another option is to concatenate strings.

sTablePrefix = "com.sun.star.table."
oLine = CreateUnoStruct(sTablePrefix & "BorderLine2")

Minimal documentation of Dim... As New can be found at CreateUnoStruct Function. This creates a new instance of a structure.

Otherwise, variables can be declared with a Dim Statement. Variables are not required to be declared unless Option Explicit is set. Also arrays can be defined using this statement.

2 Likes

StarBasic: No, objects need to be instantiated. Enumerations and constants can be assigned directly.
BorderLine = New com.sun.star.table.BorderLine
Dim oLine As Object: oLine = BorderLine

I did not understood how to implement sTablePrefix:

The following code did not recognize oLine. No Dim object as new is neeeded in that code?

Dim oCursor      As Object
    Dim oTable       As Object
    Dim oTableBorder As Object

     oCursor           = ThisComponent.CurrentController.ViewCursor	
     oStarTable        = com.sun.star.table
     
     
sTablePrefix = "com.sun.star.text."
oLine = CreateUnoStruct(sTablePrefix & "HoriOrientation.FULL")


      If (isEmpty(oCursor.textTable)) Then
      MsgBox "Cursor is not inside a table."
        
		 Else
 
        	oTable        							    = oCursor.TextTable
       		oTableBorder   						        = oTable.TableBorder
            ' oStarTable.BorderLineStyle 			    = 0    ' Solid 
            
            oTableBorder.IsLeftLineValid 				= True
            oTableBorder.IsRightLineValid				= True
            oTableBorder.IsTopLineValid					= True
            oTableBorder.IsBottomLineValid		   	    = True
            oTableBorder.IsHorizontalLineValid     		= True
            oTableBorder.IsVerticalLineValid       		= True
            
            oTableBorder.LeftLine.Color    			 	= cBlack
            oTableBorder.LeftLine.InnerLineWidth        = 10
            oTableBorder.RightLine.Color  			 	= cBlack
            oTableBorder.RightLine.InnerLineWidth       = 10
            oTableBorder.TopLine.Color  				= cBlack
            oTableBorder.TopLine.InnerLineWidth     	= 10
            oTableBorder.BottomLine.Color  				= cBlack
            oTableBorder.BottomLine.InnerLineWidth      = 10
            oTableBorder.HorizontalLine.Color  			= cBlack
            oTableBorder.HorizontalLine.InnerLineWidth  = 0
            oTableBorder.HorizontalLine.Color  			= cBlack
            oTableBorder.HorizontalLine.InnerLineWidth  = 0
            'oTableBorder.TopLine.OutetLineWidth  		= 111
            'oTableBorder.TopLine.LineDistance  		= 1
            
   

            oTable.TableBorder          				= oTableBorder 
            oTable.HoriOrient           				= oLine
            oTable.CollapsingBorders   		      	    = False
  
    End If

That is a constant, not a structure that you need to create an instance of like in your original question. For constants you can do this.

HoriOrientation = com.sun.star.text.HoriOrientation
oLine = HoriOrientation.FULL
1 Like

Снимок экрана от 2021-09-02 20-31-05

1 Like

A bit of housekeeping: @karolus, @estatistics, thanks for the likes on the answer, but without upvotes on the question itself, apparently it has lower sorting priority than other questions that have been upvoted. Not sure if you intended that. Also no idea how any of this affects trust levels.

Anyway, the most important thing is probably that once you decide on a solution - can be either an answer or a comment - then mark the solution.

Yes, I realize that, but I was looking for some way of creating a reference to it like in python. Maybe there is some way, such as through introspection. It almost seems like there could be a Ref() function to create a pointer. Arrays can be passed by reference for example.

Anyway, no such mechanism exists to my knowledge, which makes sense because Basic tries to keep things simple.

I think the best way is to look to the marvelous Macro Book from Andrew Pitonyak Macro Guides | LibreOffice Documentation - Your documentation for LibreOffice

Dim is standard declaration of some variables. If the variabels are simple (like integer, string etc.) then declaration is without new - dim s as string.
But if you need to use the predefined object, you must use as new. And it is not possible to short the names of the predefined objects.

1 Like