How to convert VBA code in OOo Writer?

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
kotfantazer
Posts: 3
Joined: Tue Dec 13, 2011 9:09 pm

How to convert VBA code in OOo Writer?

Post by kotfantazer »

I do not know how to convert this code. Sorry for my English.

Sub color_words()

Selection.HomeKey (wdStory)
Do Until ActiveWindow.Selection.End + 1 = ActiveDocument.Content.End
Selection.Words(1).Select ' выделяем первое слово и форматируем
Selection.Words(1).Font.Color = wdColorGreen
Selection.EndOf

Selection.Words(1).Select ' выделяем второе слово и форматируем
Selection.Words(1).Font.Color = wdColorYellow
Selection.EndOf
Loop

End Sub

Thank you very much!
Fedora 12, OpenOffice.org 3.2.1, OOO320m19 (Build:9505)
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: How to convert VBA code in OpenOffice Writer?

Post by rudolfo »

Can you please explain, what the macro is supposed to do? And preferably not in cyrillic.
As far as my VBA/MS Word Knowledge goes I understand that some of the selected text should be marked with green color and other parts with yellow color. But is it every other word, or everything green except for the last word of the selection?
What is the selection? The whole document? Or the part from the beginning of the document to the current cursor position?
We are users of OpenOffice, we might be familiar with the OOo macro languages, but don't expect us to understand MS VBA. You won't expect a British person to understand and translate French for you, won't you?
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
kotfantazer
Posts: 3
Joined: Tue Dec 13, 2011 9:09 pm

Re: How to convert VBA code in OpenOffice Writer?

Post by kotfantazer »

Oh, excuse me please!
Tasks macro: Select all even words - red, and the odd - green.
Fedora 12, OpenOffice.org 3.2.1, OOO320m19 (Build:9505)
kotfantazer
Posts: 3
Joined: Tue Dec 13, 2011 9:09 pm

Re: How to convert VBA code in OpenOffice Writer?

Post by kotfantazer »

Sub color_words()

Selection.HomeKey (wdStory) 'move to top
Do Until ActiveWindow.Selection.End + 1 = ActiveDocument.Content.End
Selection.Words(1).Select ' select the first word
Selection.Words(1).Font.Color = wdColorGreen 'select the word color
Selection.EndOf

Selection.Words(1).Select ' select the second word
Selection.Words(1).Font.Color = wdColorYellow
Selection.EndOf
Loop

End Sub
Fedora 12, OpenOffice.org 3.2.1, OOO320m19 (Build:9505)
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: How to convert VBA code in OpenOffice Writer?

Post by Charlie Young »

kotfantazer wrote:Oh, excuse me please!
Tasks macro: Select all even words - red, and the odd - green.
Well, I did one hastily, it does odd words green (RGB(0,255,0)), and even yellow (RGB(255,255,0)), but the colors are easily modified. It isn't very thoroughly tested, but have at it, and complain if necessary.

Code: Select all

Sub ColorWords
	Dim oDoc As Object
	Dim oText As Object
	Dim oCursor As Object
	Dim isGreen As Boolean, MoreText As Boolean
	
	oDoc = ThisComponent
	oText = oDoc.Text
	isGreen = True
	MoreText = True
	oCursor = oText.createTextCursor()
	oCursor.gotoStart(False)
	do while MoreText
		oCursor.gotoEndOfWord(True)
		if isGreen then
			oCursor.CharColor = RGB(0,255,0)
		else
			oCursor.CharColor = RGB(255,255,0)
		endif
		isGreen = not isGreen
		MoreText = oCursor.gotoNextWord(False)
	loop
	
End Sub
Apache OpenOffice 4.1.1
Windows XP
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: How to convert VBA code in OOo Writer?

Post by rudolfo »

Nice one, Charlie.
I have to remember this the next time when I hear someone complaining about the complex macro language of OpenOffice.
Obviously I was thinking that Selection.Words(1).Select selects both time the same thing. Who would ever think that Selection.EndOf is a movement operation. oCursor.gotoEndOfWord and oCursor.gotoNextWord are much more self documenting and readable.
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
User avatar
JohnSUN-Pensioner
Volunteer
Posts: 876
Joined: Fri Jan 14, 2011 1:21 pm
Location: Kyiv, Ukraine

Re: How to convert VBA code in OOo Writer?

Post by JohnSUN-Pensioner »

Yes, this is great! Really!
For even greater brevity, some variables can be removed:

Code: Select all

Sub reDrawWords
Dim oText As Object
Dim oCursor As Object
Dim isOdd As Boolean
Const Green = 65280
Const Red = 16711680
	oText = ThisComponent.getText()
	isOdd = True
	oCursor = oText.createTextCursor()
	oCursor.gotoStart(False)
	Do 
		oCursor.gotoEndOfWord(True)
		oCursor.CharColor = IIf(isOdd,Green,Red)
		isOdd = not isOdd
	Loop While oCursor.gotoNextWord(False)
End Sub
But the main solution is very elegant
I may not have a lot to give but what I got I'll give to you...
Apache OpenOffice 4.1.5, LibreOffice 6.4.4.2 (x64) on Windows 7
If you think that I did not answer your question, make allowances for my imperfect English
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: How to convert VBA code in OOo Writer?

Post by Charlie Young »

No need to stick to two colors.

Code: Select all

Sub RainbowColorWords
	Dim oDoc As Object
	Dim oText As Object
	Dim oCursor As Object
	Dim MoreText As Boolean
	Dim WordColor(6) As Long
	Dim c As Long, n As Long
	
	WordColor(0) = RGB(255,0,0) 'red
	WordColor(1) = RGB(255,165,0) 'orange
	WordColor(2) = RGB(255,255,0) 'yellow
	WordColor(3) = RGB(0,255,0) 'green
	WordColor(4) = RGB(0,0,255) 'blue
	WordColor(5) = RGB(75,0,130) ' indigo
	WordColor(6) = RGB(238,130,238) ' violet 
	
	n = UBound(WordColor) + 1
	oDoc = ThisComponent
	oText = oDoc.Text
	c = 0
	MoreText = True
	oCursor = oText.createTextCursor()
	oCursor.gotoStart(False)
	do while MoreText
		oCursor.gotoEndOfWord(True)
		oCursor.CharColor = WordColor(c)
		c = (c + 1) MOD n
		MoreText = oCursor.gotoNextWord(False)
	loop
	
End Sub
Yes, the indigo is shaky. :?
Apache OpenOffice 4.1.1
Windows XP
Post Reply