Page 1 of 1

Add index of pages to a textframe in Draw

Posted: Wed Mar 04, 2026 11:09 pm
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



Re: Add index of pages to a textframe in Draw

Posted: Thu Mar 05, 2026 8:24 am
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?

Re: Add index of pages to a textframe in Draw

Posted: Thu Mar 05, 2026 12:26 pm
by JeJe
Thanks, fixed those.