LO Basic Why is the Change Cell Border not written?

I’m having issues with changing cell border. I can retrieve values nicely, and write them but it will not be saved into the cell.
I tried even simple example to no luck:

Sub TestFormatCellBorders
   Dim oBL2 As New com.sun.star.table.BorderLine2
   Dim oSht As Object, oCelB As Object
   oSht  = ThisComponent.Sheets.getByName("Sh1")
   oBL2.Color = RGB(50, 100, 200)
   oBL2.LineStyle = 0
   oBL2.LineWidth = 44
   oCelB = oSht.getCellRangeByName("M14").TableBorder2
   If MsgBox ("RightLine Width =" & oCelB.RightLine.LineWidth, 1) = 2 Then Exit Sub
'   Set oCelB.RightLine = oBL2
   oCelB.RightLine.LineWidth = 44 
   oCelB.BottomLine = oBL2
   If MsgBox ("RightLine Width =" & oCelB.RightLine.LineWidth, 1) = 2 Then Exit Sub
End Sub

When it starts LIne width is 0, after change it is 44 as it should be, but it doesn’t change in the worksheet, and next time I ran macro it is displayed as 0 again

(No testing done!)

Your assignments arte made to a copy of the property structure. You need to assign the complete structure after the changes to the SheetCellRange.

Note: You also can’t changes ger work by direct assignments to the structuired property “in situ” without having made a copy first.

Check:

Sub TestFormatCellBorders
   Dim oBL2 As New com.sun.star.table.BorderLine2
'   Dim oSht As Object, oCelB As Object REM Option Explicit not set
   oSht  = ThisComponent.Sheets.getByName("Sh1")
   oBL2.Color = RGB(50, 100, 200)
   oBL2.LineStyle = 0
   oBL2.LineWidth = 44
   oRg = oSht.getCellRangeByName("M14:O20") REM Range changed by Lupp.
   oCellBorders = oRg.TableBorder2
   oCellBorders.RightLine.LineWidth = 44 
   oCellBorders.BottomLine = oBL2
   
   oRg.TableBorder2 = oCellBorders REM Indispensable!

End Sub
2 Likes

Thanks for the solution and explanation It’s working well.
Interesting though if I set

oCellBorders = oRg.TableBorder2
oCellBorders.RightLine.LineWidth = 44 
oRg.TableBorder2 = oCellBorder

works, but :

oCellBorders = oRg.TableBorder2
oCellBorders.RightLine.LineWidth = 0
oRg.TableBorder2 = oCellBorder

Doesn’t work. So for the LineWidth 0 if I set it within oBL2 it will work

   oBL2.LineWidth = 0
   oCellBorders = oRg.TableBorder2
   oCellBorders.RightLine.LineWidth = oBL2 
   oRg.TableBorder2 = oCellBorders 

Still need some time to fully comprehend why the extra step.
and how come simple one liner wouldn’t work
ThisComponent.Sheets.getByName("Sh1").getCellRangeByName("M14").TableBorder2.RightLine.LineWidth = 50

Imo there isn’t anything in the world of software anybody could “fully comprehend”. At best there is some longeing yourself around it.
In this case a clue might be:
Assignments to properties of objects are different from assignments to ordinary variables. The “paradigm” requires methods ordering the object to take a new content of the property or to deliver the current content to an external instance of the required type. To make sure that misunderstandings are simple, the method for the taking command is named “set method” while the method for the delivering command is named “get method”. You see: Even the inventors of the object oriented paradigm didn’t assume somebody intersted in what or who is the addressee of a command.
To make things more comfortable (but also less transparent to the user) ObjOri programming pretends to relieve things by “transparently” (obscurely) accepting syntactical assignments in place of the calls to the mentioned methods. However, if a property seen as a container isn’t equivalent to a simple variable but is a structure this “cosiness” doesn’t propagate to values (sub-properties) embedded into the property (mostly?).
You can’t effectively assign (e,g) a single value to an element of the .DataArray property of a range. To get more fun the attempted assignment is accepted without casting an error, but doesn’t result in the expected effect…
Well, concerning this very simple sub-topic, you surely got the definite grip on with the help of my explanations. :innocent:

(Be sure to notice that I’m not a developer nor an expert. I only was somehow interested in the development of programming for about 6 decades now.)
demoWithMacro.ods (11.2 KB)

1 Like