OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

How to run a macro on all open documents at once ? (Writer)

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
Kakao
General User
General User


Joined: 06 Oct 2009
Posts: 17

PostPosted: Wed Apr 28, 2010 2:13 pm    Post subject: How to run a macro on all open documents at once ? (Writer) Reply with quote

How to run a macro on all open documents at once ? (Writer)

1. I have recorded a macro (format in Writer ) and i want to run this in all open documents (2 or more) without having to do it in each one separately.

2. Can we make this macro more simple? (something like: Font.size =12 .color=black .name=arial e.t.c.)

Code:
sub formatdoc
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:SelectAll", "", 0, Array())

rem ----------------------------------------------------------------------
dim args1(4) as new com.sun.star.beans.PropertyValue
args1(0).Name = "CharFontName.StyleName"
args1(0).Value = ""
args1(1).Name = "CharFontName.Pitch"
args1(1).Value = 2
args1(2).Name = "CharFontName.CharSet"
args1(2).Value = -1
args1(3).Name = "CharFontName.Family"
args1(3).Value = 3
args1(4).Name = "CharFontName.FamilyName"
args1(4).Value = "Palatino Linotype"

dispatcher.executeDispatch(document, ".uno:CharFontName", "", 0, args1())

rem ----------------------------------------------------------------------
dim args2(7) as new com.sun.star.beans.PropertyValue
args2(0).Name = "LeftRightMargin.LeftMargin"
args2(0).Value = 0
args2(1).Name = "LeftRightMargin.TextLeftMargin"
args2(1).Value = 0
args2(2).Name = "LeftRightMargin.RightMargin"
args2(2).Value = 0
args2(3).Name = "LeftRightMargin.LeftRelMargin"
args2(3).Value = 100
args2(4).Name = "LeftRightMargin.RightRelMargin"
args2(4).Value = 100
args2(5).Name = "LeftRightMargin.FirstLineIndent"
args2(5).Value = 0
args2(6).Name = "LeftRightMargin.FirstLineRelIdent"
args2(6).Value = 100
args2(7).Name = "LeftRightMargin.AutoFirst"
args2(7).Value = false

dispatcher.executeDispatch(document, ".uno:LeftRightMargin", "", 0, args2())

rem ----------------------------------------------------------------------
dim args3(3) as new com.sun.star.beans.PropertyValue
args3(0).Name = "TopBottomMargin.TopMargin"
args3(0).Value = 0
args3(1).Name = "TopBottomMargin.BottomMargin"
args3(1).Value = 0
args3(2).Name = "TopBottomMargin.TopRelMargin"
args3(2).Value = 100
args3(3).Name = "TopBottomMargin.BottomRelMargin"
args3(3).Value = 100

dispatcher.executeDispatch(document, ".uno:TopBottomMargin", "", 0, args3())
rem ----------------------------------------------------------------------
dim args4(0) as new com.sun.star.beans.PropertyValue
args4(0).Name = "Bold"
args4(0).Value = false

dispatcher.executeDispatch(document, ".uno:Bold", "", 0, args4())
rem ----------------------------------------------------------------------
dim args5(0) as new com.sun.star.beans.PropertyValue
args5(0).Name = "Italic"
args5(0).Value = false

dispatcher.executeDispatch(document, ".uno:Italic", "", 0, args5())

rem ----------------------------------------------------------------------
dim args6(0) as new com.sun.star.beans.PropertyValue
args6(0).Name = "FontHeight.Height"
args6(0).Value = 12

dispatcher.executeDispatch(document, ".uno:FontHeight", "", 0, args6())

end sub
Back to top
View user's profile Send private message
gurfle
General User
General User


Joined: 16 Feb 2010
Posts: 7
Location: Seattle

PostPosted: Wed Apr 28, 2010 9:20 pm    Post subject: Reply with quote

Assuming you want to operate only on Text Documents. . .
Code:

Sub FormatAll()
Dim Doc As Object
dim Enum0 as object
Dim Enum1 As Object
Dim Enum2 As Object
Dim TextElement As Object
Dim TextPortion As Object
Enum0 = StarDesktop.getComponents.createEnumeration
while Enum0.hasMoreElements
   doc = Enum0.nextElement
   if doc.ImplementationName = "SwXTextDocument" then
     Enum1 = Doc.Text.createEnumeration
     While Enum1.hasMoreElements
           TextElement = Enum1.nextElement
           If TextElement.supportsService("com.sun.star.text.Paragraph") Then
                   Enum2 = TextElement.createEnumeration
                   While Enum2.hasMoreElements
                          TextPortion = Enum2.nextElement
                          TextPortion.CharHeight = 12
                          TextPortion.CharColor = rgb(0,0,0)
                          TextPortion.CharFontName = "arial"
                   Wend
           End If
     Wend
   endif
wend
end Sub

Or get rid of the check for "SwXTextDocument" if you want to catch all desktop components (could be a problem if you have the IDE open).
_________________
Nicholas Dreyer
AMD sempron 3.4GHz, 1G RAM, nForce3-250Gb motherboard
Linux Debian 5.03 (Lenny)
OpenOffice 3.1.1
Back to top
View user's profile Send private message Visit poster's website
Kakao
General User
General User


Joined: 06 Oct 2009
Posts: 17

PostPosted: Fri Apr 30, 2010 2:03 am    Post subject: Yes Reply with quote

Yes!!!! Thats great.
Thanks.

p.s. Litle more help?
This macro choose and change one by one the fonts(letters-numbers). This is very slow.

1. Can you insert a command to select all text? Its more simple and fast.

2. Command for "normal" font??? (not italic not bold)
Back to top
View user's profile Send private message
gurfle
General User
General User


Joined: 16 Feb 2010
Posts: 7
Location: Seattle

PostPosted: Fri Apr 30, 2010 9:30 pm    Post subject: Reply with quote

Quote:
2. Command for "normal" font??? (not italic not bold)
Code:
TextPortion.CharWeight = 100
Quote:
1. Can you insert a command to select all text? Its more simple and fast.
The first statements in the code of your original post:
Code:
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(document, ".uno:SelectAll", "", 0, Array())
Does select all text.

I am also relatively new to Macros for Text documents, and don't know how to work with selected text like this. I just adapted code found on page 113 in http://dlc.sun.com/pdf//817-1826/817-1826.pdf to arrive at the piece I posted above. You might want to look at it to familiarize yourself with the Macro language
Your original code from the macro recorder, as awkward as it may be, might be essentially all there is for working on the entire selection. This quote from page 104 of the document I refer to above is what makes me suspect that it is hard to manipulate text selections:
Quote:
The design of the StarOffice 7 API for StarOffice Writer differs from that of the
previous version. The old API version concentrated on work with the Selection
object, which was heavily oriented towards the idea of the user interface for end
users, which focused on mouse-controlled highlighting work.
The StarOffice 7 API has replaced these connections between user interface and
programmer interface. The programmer therefore has parallel access to all parts of
an application and can work with objects from different sub-sections of a
document at the same time. The old Selection objec t is no longer available.


Maybe others can chime in on this. You might also pose the question at http://user.services.openoffice.org/en/forum/viewforum.php?f=5&sid=4812f88864460c45ff207ea935cd1c1d/. It is a little more active than this forum.

Good luck
_________________
Nicholas Dreyer
AMD sempron 3.4GHz, 1G RAM, nForce3-250Gb motherboard
Linux Debian 5.03 (Lenny)
OpenOffice 3.1.1
Back to top
View user's profile Send private message Visit poster's website
Kakao
General User
General User


Joined: 06 Oct 2009
Posts: 17

PostPosted: Sat May 01, 2010 7:53 am    Post subject: Reply with quote

Thanks again.

I still have the 1st problem:

Quote:
This macro choose and change one by one the fonts(letters-numbers). This is very slow.

1. Can you insert a command to select all text? Its more simple and fast.


Can someone help for this?

My recorded macro do my job, but, i must open, run, save and close all documents one by one. That's i wand to avoid.
Back to top
View user's profile Send private message
gurfle
General User
General User


Joined: 16 Feb 2010
Posts: 7
Location: Seattle

PostPosted: Sun May 02, 2010 6:38 pm    Post subject: Reply with quote

My only other suggestion would be, as I said:
Quote:
You might also pose the question at http://user.services.openoffice.org/en/forum/viewforum.php?f=5&sid=4812f88864460c45ff207ea935cd1c1d/. It is a little more active than this forum

Just sign up by clicking on "register", and submit your questions.
_________________
Nicholas Dreyer
AMD sempron 3.4GHz, 1G RAM, nForce3-250Gb motherboard
Linux Debian 5.03 (Lenny)
OpenOffice 3.1.1
Back to top
View user's profile Send private message Visit poster's website
Kakao
General User
General User


Joined: 06 Oct 2009
Posts: 17

PostPosted: Mon May 03, 2010 8:57 am    Post subject: Reply with quote

I have find something for Ms Office:

Code:
Sub FormatAllFiles()
    myDirectory = "C:\Users\kakao\Desktop\TestFolder" 'where is the folder
    ChangeFileOpenDirectory myDirectory

    Dim CurrFile As String
    CurrFile = Dir(myDirectory & "\*.doc")   'what type of files
   
    Do While CurrFile <> ""

Documents.Open FileName:=CurrFile   'Open a file



Call Format      'Then call the macro "Format" to format this file

'save changes 1st ask for save, 2nd save and colse without ask
'Documents.Close SaveChanges:=wdPromptToSaveChanges
Documents.Close SaveChanges:=wdSaveChanges


CurrFile = Dir 'Call the Dir command again to get the next filename.
    Loop
End Sub


This macro run a macro in all files (.doc) in a specified folder (TestFolder).
This do my job greate but work only in MS Office.

Please Help....
Back to top
View user's profile Send private message
gurfle
General User
General User


Joined: 16 Feb 2010
Posts: 7
Location: Seattle

PostPosted: Sun May 09, 2010 12:23 pm    Post subject: Reply with quote

Kakao:

I just discovered how to set a whole group of properties at once to an entire paragraph using "setpropertyvalues". It does not appear possible to do this on the entire document text, however, nor with selected text.

Some tests I did showed improved the speed by between 2.5 and 63 times using the following imporvement over the code I posted in my original answer.

Code:
Sub FormatAll()
Dim Doc As Object
dim Enum0 as object
Dim Enum1 As Object
Dim TextElement As Object
Dim StartTime as double
Enum0 = StarDesktop.getComponents.createEnumeration
while Enum0.hasMoreElements
   doc = Enum0.nextElement
   if doc.ImplementationName = "SwXTextDocument" then
     StartTime=cdbl(getsystemticks)/1000
     Enum1 = Doc.Text.createEnumeration
     While Enum1.hasMoreElements
           TextElement = Enum1.nextElement
           If TextElement.supportsService("com.sun.star.text.Paragraph") Then _
             textelement.setpropertyvalues(array("CharColor","CharFontName", _
                                                 "CharHeight","CharWeight"), _
                                           array(rgb(0,0,0),"arial",12,100))
     Wend
     print "Time to reformat " & doc.title & ": " & _
             format(cdbl(getsystemticks)/1000- StartTime,"#,###.000") & " Seconds."
   endif
wend
end Sub

_________________
Nicholas Dreyer
AMD sempron 3.4GHz, 1G RAM, nForce3-250Gb motherboard
Linux Debian 5.03 (Lenny)
OpenOffice 3.1.1
Back to top
View user's profile Send private message Visit poster's website
Kakao
General User
General User


Joined: 06 Oct 2009
Posts: 17

PostPosted: Tue Jun 08, 2010 2:19 pm    Post subject: no news Reply with quote

nothing new?
Someone to write this VBA code for OO?

Code:
Sub FormatAllFiles()
   
  myDirectory = "C:\Users\kakao\Desktop\MyFolder" 'where is the folder
    ChangeFileOpenDirectory myDirectory

    Dim CurrFile As String
    CurrFile = Dir(myDirectory & "\*.doc")   'what type of files
   
    Do While CurrFile <> ""

    Documents.Open FileName:=CurrFile   'Open a file

    Call Format      'Then call the macro "Format" to format this file

    Documents.Close SaveChanges:=wdSaveChanges 'save the changes and close this file

    CurrFile = Dir 'Call the Dir command again to get the next filename.
  Loop

End Sub
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group