Macro for align table in Writer

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Heike
Posts: 2
Joined: Thu May 11, 2017 6:23 pm

Macro for align table in Writer

Post by Heike »

Hello,

at first, sorry for my bad english, I am German, but I try to get help in this forum.

I'd like to creat a macro for a table in writer to make a left align and set a left margin. The macro-recorder can not record the way over the tabledialoge. So I try to write it myself in the macro.

The dispatcher command for left margin have I found, for example:

Code: Select all

dim args1(4) as new com.sun.star.beans.PropertyValue
args1(0).Name = "LeftRightMargin.LeftMargin"
args1(0).Value = 1499
args1(2).Name = "LeftRightMargin.RightMargin"
args1(2).Value = 0
args1(3).Name = "LeftRightMargin.LeftRelMargin"
args1(3).Value = 100
args1(4).Name = "LeftRightMargin.RightRelMargin"
args1(4).Value = 100

dispatcher.executeDispatch(document, ".uno:LeftRightMargin", "", 0, args1())
But I search the command for set a align "from left" for the Table .

Look at this picture.
dialog-table-format.JPG
Does anybody have an idea?

Best regards Heike
OpenOffice 4.1 on Windows 8.1
User avatar
Hagar Delest
Moderator
Posts: 32658
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: macro for align table in writer

Post by Hagar Delest »

Hi and welcome to the forum!

Here is the code I use to apply a left margin on a table. It should be easy to customize it.

Code: Select all

Sub IndentTable
Dim oVC as object		' Cursor in the document
Dim oTable as object	' The table where is the cursor
Dim oRow as object		' The table where is the cursor
Dim nRow as integer
Dim nMarg as integer
' Get the view cursor
oVC = ThisComponent.getCurrentController().getViewCursor()
' Verify that the cursor is in a text table.
If IsNull(oVC.TextTable) OR IsEmpty(oVC.TextTable) Then
	msgbox "No text table is selected"
	Exit Sub
End If
oTable = oVC.TextTable
nMarg = oTable.LeftMargin
oTable.HoriOrient = 0
oTable.LeftMargin = nMarg + 500
oTable.RightMargin = 0
'oTable.HoriOrient = 1
End Sub
See: https://www.openoffice.org/api/docs/com ... ation.html for the alignment options.

Please add [Solved] at the beginning of the title in your first post (top of the topic) with the edit button if your issue has been fixed.
LibreOffice 7.6.2.1 on Xubuntu 23.10 and 7.6.4.1 portable on Windows 10
Heike
Posts: 2
Joined: Thu May 11, 2017 6:23 pm

Re: Macro for align table in Writer

Post by Heike »

Hello Hagar,

thanks for your welcome and for your help.

I test your code and it works for the left margin, but the last colum form the Table is placed on the right edge. I would like apply the macro on tables with various dimension, so I can't set a fixed right margin. I hope you can understand my plan.

For a better understanding here an image.
tabelle-macro.JPG
best regards Heike
OpenOffice 4.1 on Windows 8.1
User avatar
Hagar Delest
Moderator
Posts: 32658
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: Macro for align table in Writer

Post by Hagar Delest »

If you want to center the table, try:

Code: Select all

Sub IndentTable2
Dim oVC as object      ' Cursor in the document
Dim oTable as object   ' The table where is the cursor
Dim oRow as object      ' The table where is the cursor
Dim nRow as integer
Dim nLeft, nRight as integer
' Get the view cursor
oVC = ThisComponent.getCurrentController().getViewCursor()
' Verify that the cursor is in a text table.
If IsNull(oVC.TextTable) OR IsEmpty(oVC.TextTable) Then
   msgbox "No text table is selected"
   Exit Sub
End If
oTable = oVC.TextTable
nLeft = oTable.LeftMargin
nRight = oTable.RightMargin
oTable.LeftMargin = (nLeft + nRight)/2
oTable.RightMargin = (nLeft + nRight)/2
End Sub
LibreOffice 7.6.2.1 on Xubuntu 23.10 and 7.6.4.1 portable on Windows 10
Post Reply