Ask Your Question
0

Macro code to switch between open Writer documents [closed]

asked 2016-10-02 12:04:07 +0200

Mr Spock gravatar image

I have several documents open. I need to reference bookmarks and tables in any document from any other document. For example, I have six documents open. From doc #2 I want to reference bookmarks and tables in doc #5. From doc #3 reference bookmarks in doc #1 and tables in doc #4. Macro code can be executed from any document.

Here is where I have got to: This allows me to loop through open documents and get window names

Frames = Stardesktop.Frames
For i = 0 to Frames.Count - 1
   Frame = Frames.getByIndex(i)
   print Frame.Title
       ' code here to make this doc active
next i

Am I one the right path using Stardesktop.Frames? What code will make the document the active one?

Is there a better way?

All advice and assistance greatly appreciated

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Mr Spock
close date 2016-10-07 13:33:23.511962

2 Answers

Sort by » oldest newest most voted
2

answered 2016-10-02 13:04:53 +0200

Lupp gravatar image

updated 2016-10-02 14:09:16 +0200

Extremely reduced for a case where the StarDesktop has exactly two frames (say two opened documents, BASIC IDE not opened!).

Sub FramesTest
Dim Frames As Object, Frame0 As Object, Frame1 As Object
Frames = StarDesktop.GetFrames()
Frame0 = Frames(0)
Print Frame0.Title
Frame1 = Frames(1)
Print Frame1.Title
Frame0.Activate
Frame0.ContainerWindow.Visible=True
Frame0.ContainerWindow.ToFront()
End Sub

Or a bit more elaborate:

Sub PickFrameToFront()
Dim Frames As Object, fTally As Long, j As Long, Frame As Object
Frames = StarDesktop.GetFrames()
fTally = Frames.Count
For j = 0 To fTally - 1
    Frame = Frames(j)
    If MsgBox("Shall the component " & Frame.Title & " go to front?",_
        MB_YESNO + MB_DEFBUTTON2 + MB_ICONQUESTION) = IDYES Then
        If Frame.Controller.Model.SupportsService("com.sun.star.text.TextDocument") Then
            Frame.Activate
            Frame.ContainerWindow.Visible = True
            Frame.ContainerWindow.ToFront()
            Exit For
        Else
            MsgBox("The frame did not belong to a text document. Please continue.")
        End If
    End If
Next j
End Sub
edit flag offensive delete link more
0

answered 2016-10-03 02:44:49 +0200

Mr Spock gravatar image

Using Frames led us up a blind alley!

I have made progress! After numerous Google searches and consulting Andrew Pitonyak's excellent reference Open Office Macros Explained, I have reached this point:

GlobalScope.BasicLibraries.LoadLibrary("Tools")     'Contains FileNameOutOfPath
vComponents = StarDesktop.getComponents()           'Get all the components
vDocs = vComponents.createEnumeration()             'Enumerate them

Do While vDocs.hasMoreElements()                    'While there are more
    vDoc = vDocs.nextElement()                      'Get the next component
    If vDoc.SupportsService("com.sun.star.text.TextDocument") Then
        if vDoc.GetBookmarks().GetCount <> 0 then
            vBookmarks = vDoc.GetBookmarks()
            for i = 0 to vBookmarks.getCount() -1
                vBM = vBookmarks.getbyIndex(i)
                vBM.getAnchor.setString("(" & Now() & ": " & vBM.Name & ")")
            next
        end if
    End If
Loop

This code loops through my open documents, finds all bookmarks and inserts text at those bookmarks. My next challenge is manipulating tables in the documents.

During this process I came across numerous comments from others about how poorly the Object model is documented. The most frustrating aspect is no consistency. EG: using .createEnumeration() works in some places but not others. One commentor made the observation that a big issue holding Open Office/Libre Office back in the commercial world is the very steep learning curve for OO/LO Basic vs MS Office VBA. I agree. I have written numerous macros in MS VBA. In the VBA object browser you can inspect any object to determine the correct syntax.

edit flag offensive delete link more

Comments

@Mr Spock: I do not clearly see your intentions. And: IMO one of the serious flaws of the LibreOffice project is this poor askbot site. One of the (intended?) implications is that no continuous discussion is supported. "Question - Answer - Die" is the thread model here. Unfortunately the better libreofficeforum.org recently shut down. There was an excellent article by @oweng also discussing the VBA/ooBASIC aspects...

Discussing threads are much better supported at forum.openoffice.org/en

Lupp gravatar imageLupp ( 2016-10-03 13:21:04 +0200 )edit

@Lupp: I agree about this site and on-going dialog. I attempted to post my last answer as a comment and hit the character limit. I came here because I am still unsure of the difference (if any) between Libre Office, Open Office, Star Office and all the other flavours. Meantime I will still search for the "Dummies Guide to OpenOffice Object Model".

Mr Spock gravatar imageMr Spock ( 2016-10-03 23:52:34 +0200 )edit

@Mr Spock: (You may upvote my question "Why ... not structured better?" or one of the answers there.)
StarOffice is long gone. The differences between AOO and LibO, however get more and more relevant, in specific cocerning the UI.
On the other hand it no longer seems impossible that AOO will no longer improve and release new versions. (See next comment ...

Lupp gravatar imageLupp ( 2016-10-04 00:07:31 +0200 )edit

... here comes.)

The Apache OpenOffice forum, however, is very valuable and also accepts topics concerning LibO. If usage of AOO should go down one day that forum shall not go down, too! Therefore I also contribute there. You may ask there occasionally to get an impression. Note the announcment (white on light blue) on their front page.

Lupp gravatar imageLupp ( 2016-10-04 00:13:04 +0200 )edit

Question Tools

1 follower

Stats

Asked: 2016-10-02 12:04:07 +0200

Seen: 839 times

Last updated: Oct 03 '16