Add index of pages to a textframe in Draw

Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This section is not for asking questions about writing your own macros.
Post Reply
JeJe
Volunteer
Posts: 3134
Joined: Wed Mar 09, 2016 2:40 pm

Add index of pages to a textframe in Draw

Post by JeJe »

Add index of pages to a textframe in Draw
Select the chosen frame (formatted with the font/columns etc desired). Click on the border of the frame NOT the text in the frame, and run the first sub.

Edit: fixes thanks to cwolan, OpenOffice uses "Slide" as a default name instead of "Page" and made modified = true after running.

Code: Select all

 Sub AddIndexToTextFrame() 'Select the frame by clicking on the border - NOT the text in the frame
dim pagename as string,oDoc,oDrawPages,otext,i as long,url,representation
    oDoc = ThisComponent
    oDrawPages = odoc.drawpages
    otext = thiscomponent.currentselection.getbyindex(0).gettext
      otext.string = ""
    For i = 0 To  oDrawPages.getCount() - 1
        pageName = oDrawPages.getByIndex(i).Name
        'If pageName = "" or 'NOT SURE IF THIS EVER OCCURS 
        if pagename = ("page" & i+1) then pageName = oDrawPages.getByIndex(i).LinkDisplayName
        url = "#" & pagename
        representation = pagename
        InsertHyperlinkInTextShape2(odoc,otext,url,representation)
        oText.insertString(oText.getEnd(), Chr(13), False)
    Next i
   thiscomponent.modified = true
   msgbox "Done"
   
End Sub

sub InsertHyperlinkInTextShape2(odoc,oText,url,representation)
dim field
field = odoc.createInstance("com.sun.star.text.textfield.URL") 
with field
.url = url
.Representation =representation
.format = 2
.TargetFrame ="_self"
end with
oText.insertTextContent(otext.getend, field, False) 
end sub


Last edited by JeJe on Thu Mar 05, 2026 12:26 pm, edited 2 times in total.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
cwolan
Posts: 217
Joined: Sun Feb 07, 2021 3:44 pm

Re: Add index of pages to a textframe in Draw

Post by cwolan »

(1)
For Apache OpenOffice and default page names ("Slide 1", "Slide 2" etc.): the inserted links won't work.

(2)
The document status does not change after running the macro (see the "Save" icon). Is it acceptable?
OpenOffice 1.1.5 – 4.1.16
LibreOffice 3.3.0.4 – 26.2
Windows 7,10,11 64-bit
JeJe
Volunteer
Posts: 3134
Joined: Wed Mar 09, 2016 2:40 pm

Re: Add index of pages to a textframe in Draw

Post by JeJe »

Thanks, fixed those.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply