Hello,
I need a simple macro: I want to go through the entire document in OpenOffice Writer and modify the properties of every paragraph's top border (but if some paragraph doesn't already have a border, I don't want to add it). Unfortunately I'm not really familiar with OpenOffice Basic's syntax and I'd rather not learn all of it just for this one little task.
For now I have the following macro, but it's obviously not working:
Sub Test
Dim Doc As Object
Dim Enum1 As Object
Doc = ThisComponent
Enum1 = Doc.Text.createEnumeration
While Enum1.hasMoreElements
Paragraph = Enum1.nextElement()
If Paragraph.TopBorder <> Array(0,0,0,0) Then
Paragraph.TopBorder = Array(0,0,18,0)
Paragraph.TopDistance = 41
EndIf
Wend
End Sub
Could anyone please tell me what I should change in this code to make it work?
Last edited by Nukon on Wed Sep 20, 2017 9:25 pm, edited 4 times in total.
Have you tried to use the Styles instead of that direct formatting Macro-solution?
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Using 'Styles' seems to add borders even to paragraphs which didn't have them before and I just want to edit the existing ones.
You must use more than one different styles in a well-formatted document...
(In this case you can modify the desired styles only by a macro or manually.)
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Using 'Styles' seems to add borders even to paragraphs which didn't have them before and I just want to edit the existing ones.
You must use more than one different styles in a well-formatted document...
(In this case you can modify the desired styles only by a macro or manually.)
The question stated in the topic is how to make such a macro and your answer is to create a macro? If I knew how to make it, I wouldn't start this topic, would I? And my document is too long to do it manually.
There are many examples how to modify a style by macro. Here are one of them: viewtopic.php?t=48960
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
RoryOF wrote:If using Styles adds borders to previously unbordered paragraphs, then Add them, then edit the Style(s) to undo the borders.
I'm not sure what you mean by this. If I add borders to each paragraph and then remove borders from each paragraph, wouldn't I be left with no borders at all?
Zizi64 wrote:There are many examples how to modify a style by macro. Here are one of them: viewtopic.php?t=48960
Knowing how to modify the style alone is not enough as I only want to modify the style of paragraphs which already have a border and I don't know how to check this.
Knowing how to modify the style alone is not enough as I only want to modify the style of paragraphs which already have a border and I don't know how to check this.
Similarly as you can set it. You need study the API functions.
And please download and install the MRI and/or the XrayTool object examination extension, if you want to know the properties/methodes/interfaces of the programming objects.
Last edited by Zizi64 on Wed Sep 20, 2017 6:07 pm, edited 1 time in total.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
You asked how to modify every paragraph's border; what I described will do that (just now tested for confirmation).
If your document is Style based and direct formatting was used to apply borders to some paragraphs, you could select the entire document (Ctrl A) and then press Ctrl M, which will remove the direct formatting that applied the borders. Note that this will also remove other direct formatting.
Knowing how to modify the style alone is not enough as I only want to modify the style of paragraphs which already have a border and I don't know how to check this.
Similarly as you can set it. You need study the API functions.
And please download and install the MRI and/or the XrayTool object examination extension, if you want to know the properties/methodes/interfaces of the programming objects.
I have one simple macro to make. I have code that seems to make sense, but obviously has some syntax errors because I haven't made any macro for OpenOffice before. I hoped that someone could help me fix this code, so I wouldn't have to learn the entire API and download various extentions just to perform this one task. I'm sorry, but your advices are not helpful.
RoryOF wrote:You asked how to modify every paragraph's border; what I described will do that (just now tested for confirmation).
If your document is Style based and direct formatting was used to apply borders to some paragraphs, you could select the entire document (Ctrl A) and then press Ctrl M, which will remove the direct formatting that applied the borders. Note that this will also remove other direct formatting.
It doesn't change the style of existing borders in my document. My document is a .docx, but because of some limitations in MS Word, I have to use OpenOffice to generate a .pdf from it and because of some incompatibilities between MS Word and OpenOffice, I have to edit the borders before generating the .pdf.
With the .docx you should be able to generate a PDF by installing a virtual PDF printer; or look at http://www.zamzar.com who offer free online file format conversions. I haven't checked, but I am confident they will offer .docx to PDF.
In this case the macro probably will not work, because the converted "styles" and manual formatting properties are not 100% compatible with the ODF type ones.
Please upload a real (docx type) sample document here.
but because of some limitations in MS Word, I have to use OpenOffice to generate a .pdf
There are third party virtual pdf printer softwares. They can work with MSO too.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
RoryOF wrote:With the .docx you should be able to generate a PDF by installing a virtual PDF printer; or look at http://www.zamzar.com who offer free online file format conversions. I haven't checked, but I am confident they will offer .docx to PDF.
Zizi64 wrote:
My document is a .docx,
In this case the macro probably will not work, because the converted "styles" and manual formatting properties are not 100% compatible with the ODF type ones.
Please upload a real (docx type) sample document here.
but because of some limitations in MS Word, I have to use OpenOffice to generate a .pdf
There are third party virtual pdf printer softwares. They can work with MSO too.
It's more complicated than this, the way I want to do this seems to be the only solution; using a PDF printer within MS Word has the same limitations as MS Word itself.
Data included in the original document is confidential, so I cannot upload it here; I quickly generated a new document which has the same formatting. https://ufile.io/was7x
In Find box ^$ to find empty lines. Drop More options and select Regular expressions.
Press Find all
Press Close to exit Find and Replace. Notice that all blank lines are now selected.
Press Ctrl M to remove direct formatting (i.e., the applied top border.)
All should now be as you wish.
Edit: I don't see anything in the sample file that could not be done easily in OpenOffice. Could you, out of interest, specify what .docx could do that OO couldn't?
RoryOF wrote:On your sample file, what worked for me was this:
In Find box ^$ to find empty lines. Drop More options and select Regular expressions.
Press Find all
Press Close to exit Find and Replace. Notice that all blank lines are now selected.
Press Ctrl M to remove direct formatting (i.e., the applied top border.)
All should now be as you wish.
Edit: I don't see anything in the sample file that could not be done easily in OpenOffice. Could you, out of interest, specify what .docx could do that OO couldn't?
So I removed formatting from all empty lines, including font type and size (which is also relevant because it changes the distance between two parts of the document), but I don't see how it makes me any closer to changing parameters of all paragraph's borders that existed before doing this.
And do you mean why I decided to make this document in MS Word rather than OpenOffice? There was no specific reason, I just didn't know about MS Word's limits at the time I made it.
Color long 0
InnerLineWidth integer 0
OuterLineWidth integer 0
LineDistance integer 0
LineStyle integer 0
LineWidth long 0
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Sub Test
Dim Doc As Object
Dim Enum1 As Object
Doc = ThisComponent
Enum1 = Doc.Text.createEnumeration
While Enum1.hasMoreElements
Paragraph = Enum1.nextElement()
If Paragraph.TopBorder.OuterLineWidth <> 0 Then
Paragraph.TopBorder.OuterLineWidth = 18
Paragraph.TopBorderDistance = 41
EndIf
Wend
End Sub
but it tells me there's no property or method called 'TopBorder'.
Your document contains some tables and some normal paragraphs. The paragraphs in a Table have not TopBorder property. I just XRAY-ed this information...) Therefore you need examine if the actual paragraph is "SwXParagraph" type or not...
REM ***** BASIC *****
Option explicit
Sub Test
Dim oDoc As Object
Dim oEnum1 As Object
Dim oParagraph As object
Dim oBorderStruct As object
oDoc = ThisComponent
oEnum1 = oDoc.Text.createEnumeration
While oEnum1.hasMoreElements
oParagraph = oEnum1.nextElement()
'Xray oParagraph
If oParagraph.ImplementationName = "SwXParagraph" then 'because the paragraphs in a Table have not TopBorder property
oBorderStruct = oParagraph.TopBorder
'Xray oBorderStruct
If oBorderStruct.Color + oBorderStruct.InnerLineWidth +_
oBorderStruct.OuterLineWidth + oBorderStruct.LineDistance +_
oBorderStruct.LineStyle + oBorderStruct.LineWidth <> 0 Then
'Xray oBorderStruct
Xray oParagraph
oParagraph.TopBorderDistance = 41
end if
end if
Wend
End Sub
I never known this informations, but i used the XRAY, and i got these informations. And you can to do this too.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Zizi64 wrote:Your document contains some tables and some normal paragraphs. The paragraphs in a Table have not TopBorder property. I just XRAY-ed this information...) Therefore you need examine if the actual paragraph is "SwXParagraph" type or not...
REM ***** BASIC *****
Option explicit
Sub Test
Dim oDoc As Object
Dim oEnum1 As Object
Dim oParagraph As object
Dim oBorderStruct As object
oDoc = ThisComponent
oEnum1 = oDoc.Text.createEnumeration
While oEnum1.hasMoreElements
oParagraph = oEnum1.nextElement()
'Xray oParagraph
If oParagraph.ImplementationName = "SwXParagraph" then 'because the paragraphs in a Table have not TopBorder property
oBorderStruct = oParagraph.TopBorder
'Xray oBorderStruct
If oBorderStruct.Color + oBorderStruct.InnerLineWidth +_
oBorderStruct.OuterLineWidth + oBorderStruct.LineDistance +_
oBorderStruct.LineStyle + oBorderStruct.LineWidth <> 0 Then
'Xray oBorderStruct
Xray oParagraph
oParagraph.TopBorderDistance = 41
end if
end if
Wend
End Sub
I never known this informations, but i used the XRAY, and i got these informations. And you can to do this too.
Thank you very much! I edited your code slightly (there was some other problem there as well) and it finally started modifying borders' properties. I'm still not able to get the expected result (I want it to correctly convert to .pdf and I was able to do this when I manually removed and re-added borders), but I think it's just a matter of adding some more lines to the code.
Nukon wrote:
So I removed formatting from all empty lines, including font type and size (which is also relevant because it changes the distance between two parts of the document), but I don't see how it makes me any closer to changing parameters of all paragraph's borders that existed before doing this.
All the empty lines had been direct formatted to 9pt, overriding the 11pt of the paragraph style definition. With all these empty paragraphs selected (after removal of the direct formatting paragraph borders by the Ctrl M), simple select 9 in the font size dropdown to reapply the 9 pt direct formatting to these lines.
Okay, so it looks like there are still some problems. Changing the value of oParagraph.TopBorder.OuterLineWidth doesn't seem to do anything.
RoryOF wrote:
Nukon wrote:
So I removed formatting from all empty lines, including font type and size (which is also relevant because it changes the distance between two parts of the document), but I don't see how it makes me any closer to changing parameters of all paragraph's borders that existed before doing this.
All the empty lines had been direct formatted to 9pt, overriding the 11pt of the paragraph style definition. With all these empty paragraphs selected (after removal of the direct formatting paragraph borders by the Ctrl M), simple select 9 in the font size dropdown to reapply the 9 pt direct formatting to these lines.
So this just removed each paragraph's border. I don't see how I could make use of it to bring these borders back and edit their properties. Undoing changes in Styles undoes entire operation.
REM ***** BASIC *****
Option explicit
Sub Test
Dim oDoc As Object
Dim oEnum1 As Object
Dim oParagraph As object
Dim oBorderStruct As object
Dim lBorder as New com.sun.star.table.BorderLine '************************** new
oDoc = ThisComponent
oEnum1 = oDoc.Text.createEnumeration
While oEnum1.hasMoreElements
oParagraph = oEnum1.nextElement()
'Xray oParagraph
If oParagraph.ImplementationName = "SwXParagraph" then 'because the paragraphs in a Table have not TopBorder property
oBorderStruct = oParagraph.TopBorder
'Xray oBorderStruct
If oBorderStruct.Color + oBorderStruct.InnerLineWidth +_
oBorderStruct.OuterLineWidth + oBorderStruct.LineDistance +_
oBorderStruct.LineStyle + oBorderStruct.LineWidth <> 0 Then
'Xray oBorderStruct
Xray oParagraph
oParagraph.TopBorderDistance = 100
lBorder.OuterLineWidth = 50 '************************** new
lBorder.LineDistance = 0 '************************** new
oParagraph.TopBorder = lBorder '************************** new
end if
end if
Wend
End Sub
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
REM ***** BASIC *****
Option explicit
Sub Test
Dim oDoc As Object
Dim oEnum1 As Object
Dim oParagraph As object
Dim oBorderStruct As object
Dim lBorder as New com.sun.star.table.BorderLine '************************** new
oDoc = ThisComponent
oEnum1 = oDoc.Text.createEnumeration
While oEnum1.hasMoreElements
oParagraph = oEnum1.nextElement()
'Xray oParagraph
If oParagraph.ImplementationName = "SwXParagraph" then 'because the paragraphs in a Table have not TopBorder property
oBorderStruct = oParagraph.TopBorder
'Xray oBorderStruct
If oBorderStruct.Color + oBorderStruct.InnerLineWidth +_
oBorderStruct.OuterLineWidth + oBorderStruct.LineDistance +_
oBorderStruct.LineStyle + oBorderStruct.LineWidth <> 0 Then
'Xray oBorderStruct
Xray oParagraph
oParagraph.TopBorderDistance = 100
lBorder.OuterLineWidth = 50 '************************** new
lBorder.LineDistance = 0 '************************** new
oParagraph.TopBorder = lBorder '************************** new
end if
end if
Wend
End Sub
Thank you very much again, it finally works as intended! I don't think that without any experience in creating OpenOffice macros I would be able to do all of it, even if I used XRay.
This is why this office suite is based on styles. Using styles, you hardly ever need any macros.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
This is why this office suite is based on styles. Using styles, you hardly ever need any macros.
+1
I don't think that without any experience in creating OpenOffice macros I would be able to do all of it, even if I used XRay.
You tried to accomplish this task with macros ...
Use the Styles if you have not enough informations about the object oriented programming, and the API functions of the AOO/LO.
(Or use the suggested helper tools... The helper tools will not create the macro code, but they will give you many useful informations.)
About my macro solution:
If you will set once the border parameters of the desired paragraphs to 0 value, you will never found them again with my/your Subroutine. Therefore it is better to use the NAMED styles.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.