[Solved] Create macro to insert index entry

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
sh1
Posts: 14
Joined: Sun Feb 22, 2009 1:19 am

[Solved] Create macro to insert index entry

Post by sh1 »

I would like to create an alphabetical index for a long document. At the moment I have to highlight the words I would like to index, click insert>indexes and tables>entry then insert. I could not create a macro or shortcut for this. Can anyone help?
Thanx
Last edited by Hagar Delest on Tue Feb 24, 2009 10:54 am, edited 1 time in total.
Reason: tagged [Solved].
OOo 3.0.X on Ms Windows XP
User avatar
Hagar Delest
Moderator
Posts: 32627
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: Create macro to insert index entry

Post by Hagar Delest »

See here for starters: [Solved] How do I learn Starbasic? and post again if you have difficulties in the construction of your macro.
LibreOffice 7.6.2.1 on Xubuntu 23.10 and 7.6.4.1 portable on Windows 10
sh1
Posts: 14
Joined: Sun Feb 22, 2009 1:19 am

Re: Create macro to insert index entry

Post by sh1 »

Thanks, I'll give that a try and let you know.
OOo 3.0.X on Ms Windows XP
sh1
Posts: 14
Joined: Sun Feb 22, 2009 1:19 am

Re: Create macro to insert index entry

Post by sh1 »

I normally record macros by doing tools>macros>record macro etc. but this doesn't work for inserting index entries.
I tried looking up your references but it way over my head - I'm a just a beginner! :? Is there any simple way?
I also tried to see if I could customize my toolbar and add this command, but I could not find this command at all. Any ideas for dummies like me?
Thanks
OOo 3.0.X on Ms Windows XP
turtle47
Posts: 31
Joined: Tue Sep 16, 2008 3:54 pm

Re: Create macro to insert index entry

Post by turtle47 »

Hi,

try following code:

Code: Select all

Sub SetIndex
Dim oSelction as Object
Dim oBereich as Object
Dim oIndex as Object
Dim oDocument as Object
Dim oText as Object
Dim viewCursor as Object
Dim Eintrag as String
oDocument = ThisComponent
oText = oDocument.Text
oSelection = ThisComponent.CurrentController.Selection
oindex =oDocument.createInstance("com.sun.star.text.DocumentIndexMark")
viewCursor = oDocument.getCurrentController.ViewCursor
for Int1 = 0 to oSelection.Count -1
oBereich = oSelection (Int1)
if HasUnoInterfaces(oBereich,"com.sun.star.text.XTextRange") Then
oIndex.setMarkEntry(oBereich.getString)
oText.InsertTextContent(oBereich, oIndex,False)
end If
Next Int1
End Sub
Multiple selection doesn't work!!

Good luck.

Juergen
Last edited by turtle47 on Tue Feb 24, 2009 7:34 am, edited 1 time in total.
OOo 3.2.X on Ms Windows 7
sh1
Posts: 14
Joined: Sun Feb 22, 2009 1:19 am

Re: Create macro to insert index entry

Post by sh1 »

Thanks for taking your time to help me.
I tried your code but it got stuck on the last line "End Sub", I get a message "BASIC syntax error. Unexpected symbol: End Sub" Any idea whats its about?
Thank you
OOo 3.0.X on Ms Windows XP
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Create macro to insert index entry

Post by FJCC »

The statement "for Int1 = 0 to oSelection.Count -1" needs to be paired with a statement "Next Int1". I think that statement goes just before the End sub, so the code would look like.

Code: Select all

Sub SetIndex
Dim oSelction as Object
Dim oBereich as Object
Dim oIndex as Object
Dim oDocument as Object
Dim oText as Object
Dim viewCursor as Object
Dim Eintrag as String
oDocument = ThisComponent
oText = oDocument.Text
oSelection = ThisComponent.CurrentController.Selection
oindex =oDocument.createInstance("com.sun.star.text.DocumentIndexMark")
viewCursor = oDocument.getCurrentController.ViewCursor
for Int1 = 0 to oSelection.Count -1
oBereich = oSelection (Int1)
if HasUnoInterfaces(oBereich,"com.sun.star.text.XTextRange") Then
oIndex.setMarkEntry(oBereich.getString)
oText.InsertTextContent(oBereich, oIndex,False)
end If
Next Int1
End Sub
I hope turtle47 will come back and confirm that, as it is easy to misunderstand someone's else's code.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
sh1
Posts: 14
Joined: Sun Feb 22, 2009 1:19 am

Re: Create macro to insert index entry

Post by sh1 »

Thanks FJCC, I tried your addition and it seems to be working! :D
Thank you turtle47 for the code. :D
OOo 3.0.X on Ms Windows XP
turtle47
Posts: 31
Joined: Tue Sep 16, 2008 3:54 pm

Re: Create macro to insert index entry

Post by turtle47 »

@ sh1 Sorry for the mistake? :oops:

@ FJCC Thanks for correction. :idea:
OOo 3.2.X on Ms Windows 7
timur
Posts: 5
Joined: Wed Apr 15, 2009 12:57 am

Re: [Solved] Create macro to insert index entry

Post by timur »

This doesn't work for me. I select the text and run the macro. Instead of making the current selection into an index entry, it inserts the entry in front of the selection. So it's not the same behavior as Insert->Index and Tables->Entry, then clicking "Insert".

I used to know Basic, so I can debug this, but I don't know any of the OO commands. It appears that oBereich.getString is returning the selection, but I don't know what oText.InsertTextContent is supposed to do.
OOo 3.0.X on Fedora other
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: [Solved] Create macro to insert index entry

Post by FJCC »

I can get behavior similar to what you describe if I run the macro before inserting the index. Try defining the index first and then running the macro to insert entries.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
timur
Posts: 5
Joined: Wed Apr 15, 2009 12:57 am

Re: [Solved] Create macro to insert index entry

Post by timur »

I'm sorry, I don't understand what you're talking about. What do you mean by "defining the index"?
OOo 3.0.X on Fedora other
User avatar
Hagar Delest
Moderator
Posts: 32627
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: [Solved] Create macro to insert index entry

Post by Hagar Delest »

Just inserting it (even if no entries yet) perhaps?
LibreOffice 7.6.2.1 on Xubuntu 23.10 and 7.6.4.1 portable on Windows 10
timur
Posts: 5
Joined: Wed Apr 15, 2009 12:57 am

Re: [Solved] Create macro to insert index entry

Post by timur »

I'm sorry, I still don't understand what you're talking about. Insert what where?
OOo 3.0.X on Fedora other
User avatar
Hagar Delest
Moderator
Posts: 32627
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: [Solved] Create macro to insert index entry

Post by Hagar Delest »

Hmm, I just tried and I see the same. Can't managed to have the requested behavior. I think we've to wait for FJCC comments.
LibreOffice 7.6.2.1 on Xubuntu 23.10 and 7.6.4.1 portable on Windows 10
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: [Solved] Create macro to insert index entry

Post by FJCC »

First, this isn't my code, so I'm treading on thin ice. Turtle47 posted the code and all I did is point out a simple error.

The attached document has an alphabetical index that I inserted using Insert -> Indexes and Tables -> Indexes and Tables. I then highlighted the word OpenOffice and ran the macro using Tools -> Macros ->Run Macro and navigating down to the IndexExample.odt ->Standard -> TextCursor -> AddToIndex. The macro runs silently, so don't be surprised when nothing happens. Then I go to the Alphabetical Index title at the bottom of the document, right click on it and select Update Index/Table. The word or phrase that was highlighted then appears in the index with the page number.
Attachments
IndexExample.odt
(16.24 KiB) Downloaded 533 times
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
Hagar Delest
Moderator
Posts: 32627
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: [Solved] Create macro to insert index entry

Post by Hagar Delest »

Yes but timur is right. Activate the fields shading and see the difference between a word added to the index by the macro and another by the OOo menu.
LibreOffice 7.6.2.1 on Xubuntu 23.10 and 7.6.4.1 portable on Windows 10
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: [Solved] Create macro to insert index entry

Post by FJCC »

I see what you mean now. I'm afraid I'm stuck, as there is part of turtle47's code that I don't understand.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
smok2212
Posts: 18
Joined: Thu Mar 19, 2009 2:34 am

Re: [Solved] Create macro to insert index entry

Post by smok2212 »

Hi,

The code is actually working, but how do I create the index automatically? And also, how to refresh the index automatically after a new entry?

Thanks
OOo 3.0.X on Ms Windows XP
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: [Solved] Create macro to insert index entry

Post by RoryOF »

Position cursor where you want the index. Insert / Indexes and Table and select the drop down for Index.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
smok2212
Posts: 18
Joined: Thu Mar 19, 2009 2:34 am

Re: [Solved] Create macro to insert index entry

Post by smok2212 »

RoryOF, I want to do that using OOO Basic language, not manually.
OOo 3.0.X on Ms Windows XP
gorthmog
Posts: 9
Joined: Sun Apr 20, 2014 5:09 am

Re: [Solved] Create macro to insert index entry

Post by gorthmog »

I think I figured out how to insert the index entry and have it span the entire selection (not just stick it out front). The key is to *not* call setMarkEntry, and then instead, in the call to InsertTextContext, set the third argument to "True", to replace the entire selection with the DocumentIndexMark. This mimics the behavior of the Insert Index Entry dialog - if you modify the entry then it will stick the index mark in front of your selection.. But if you just use the selected text as is, then it will span the selected word, which is what you want.

Code: Select all

Sub SetIndex
  oSelection = ThisComponent.CurrentController.Selection
  oIndex =ThisComponent.createInstance("com.sun.star.text.DocumentIndexMark")
  for Int1 = 0 to oSelection.Count -1
    oSel1 = oSelection (Int1)
    if HasUnoInterfaces(oSel1,"com.sun.star.text.XTextRange") Then
      ThisComponent.Text.InsertTextContent(oSel1, oIndex, True)
    end If
  Next Int1
End Sub
Hope this helps!
-Chris
OpenOffice 4.1.1 on MacOS 10.10
stas
Posts: 1
Joined: Tue Apr 18, 2017 8:04 am

Re: [Solved] Create macro to insert index entry

Post by stas »

Thank you, gorthmog, for that macro!

I needed to automatically lowercase the index entries as they are being added for book style index table, so after much trial and error, I figured out I needed to set a public key oIndex.AlternativeText to accomplish that.

Code: Select all

Sub SetIndex
  oSelection = ThisComponent.CurrentController.Selection
  oIndex =ThisComponent.createInstance("com.sun.star.text.DocumentIndexMark")
 
  for Int1 = 0 to oSelection.Count -1
    oSel1 = oSelection (Int1)
    if HasUnoInterfaces(oSel1,"com.sun.star.text.XTextRange") Then

      rem this lowercases the entry - useful for book style index table
      oIndex.AlternativeText = LCase(oSel1.String)

      ThisComponent.Text.InsertTextContent(oSel1, oIndex, True)
   
    end If
  Next Int1
End Sub
so now if the text is: "First entry" the index entry becomes "first entry", etc.

Once added as a macro and saved, make it into a quick macro shortcut:

1. in Menu select: Tools -> Customize -> Keyboard tab
2. in column Category select: LibreOffice Macros (user -> Standard -> Module1
3. in column Function select: SetIndex
4. select a free key in "Short Cut keys" panel at the top (I had F4 free)
5. click 'Modify' && 'OK'

now F4 runs this macro.

Enjoy.
OpenOffice 5.1.6.2 on Ubuntu 16.04
Post Reply