Page 1 of 1

[Solved] Create macro to insert index entry

Posted: Sun Feb 22, 2009 1:31 am
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

Re: Create macro to insert index entry

Posted: Sun Feb 22, 2009 3:14 pm
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.

Re: Create macro to insert index entry

Posted: Sun Feb 22, 2009 7:35 pm
by sh1
Thanks, I'll give that a try and let you know.

Re: Create macro to insert index entry

Posted: Mon Feb 23, 2009 3:14 am
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

Re: Create macro to insert index entry

Posted: Mon Feb 23, 2009 12:33 pm
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

Re: Create macro to insert index entry

Posted: Tue Feb 24, 2009 12:08 am
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

Re: Create macro to insert index entry

Posted: Tue Feb 24, 2009 12:48 am
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.

Re: Create macro to insert index entry

Posted: Tue Feb 24, 2009 2:09 am
by sh1
Thanks FJCC, I tried your addition and it seems to be working! :D
Thank you turtle47 for the code. :D

Re: Create macro to insert index entry

Posted: Tue Feb 24, 2009 7:33 am
by turtle47
@ sh1 Sorry for the mistake? :oops:

@ FJCC Thanks for correction. :idea:

Re: [Solved] Create macro to insert index entry

Posted: Wed Apr 15, 2009 1:03 am
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.

Re: [Solved] Create macro to insert index entry

Posted: Wed Apr 15, 2009 4:24 am
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.

Re: [Solved] Create macro to insert index entry

Posted: Wed Apr 15, 2009 9:47 pm
by timur
I'm sorry, I don't understand what you're talking about. What do you mean by "defining the index"?

Re: [Solved] Create macro to insert index entry

Posted: Wed Apr 15, 2009 10:11 pm
by Hagar Delest
Just inserting it (even if no entries yet) perhaps?

Re: [Solved] Create macro to insert index entry

Posted: Wed Apr 15, 2009 10:23 pm
by timur
I'm sorry, I still don't understand what you're talking about. Insert what where?

Re: [Solved] Create macro to insert index entry

Posted: Wed Apr 15, 2009 10:42 pm
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.

Re: [Solved] Create macro to insert index entry

Posted: Thu Apr 16, 2009 1:02 am
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.

Re: [Solved] Create macro to insert index entry

Posted: Thu Apr 16, 2009 8:07 am
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.

Re: [Solved] Create macro to insert index entry

Posted: Thu Apr 16, 2009 2:46 pm
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.

Re: [Solved] Create macro to insert index entry

Posted: Wed Sep 30, 2009 10:20 pm
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

Re: [Solved] Create macro to insert index entry

Posted: Wed Sep 30, 2009 10:26 pm
by RoryOF
Position cursor where you want the index. Insert / Indexes and Table and select the drop down for Index.

Re: [Solved] Create macro to insert index entry

Posted: Wed Sep 30, 2009 11:18 pm
by smok2212
RoryOF, I want to do that using OOO Basic language, not manually.

Re: [Solved] Create macro to insert index entry

Posted: Thu May 15, 2014 6:39 am
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

Re: [Solved] Create macro to insert index entry

Posted: Tue Apr 18, 2017 8:15 am
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.