[Solved] Create macro to insert index entry

Creating a macro - Writing a Script - Using the API

[Solved] Create macro to insert index entry

Postby sh1 » Sun Feb 22, 2009 1:31 am

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
sh1
 
Posts: 14
Joined: Sun Feb 22, 2009 1:19 am

Re: Create macro to insert index entry

Postby Hagar Delest » Sun Feb 22, 2009 3:14 pm

See here for starters: [Solved] How do I learn Starbasic? and post again if you have difficulties in the construction of your macro.
AOO 4.1.3 on Xubuntu 17.04, (Zesty Zapus) and on Windows 7 (with winPenPack port).
User avatar
Hagar Delest
Moderator
 
Posts: 27467
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: Create macro to insert index entry

Postby sh1 » Sun Feb 22, 2009 7:35 pm

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

Postby sh1 » Mon Feb 23, 2009 3:14 am

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
sh1
 
Posts: 14
Joined: Sun Feb 22, 2009 1:19 am

Re: Create macro to insert index entry

Postby turtle47 » Mon Feb 23, 2009 12:33 pm

Hi,

try following code:
Code: Select all   Expand viewCollapse view
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
turtle47
 
Posts: 31
Joined: Tue Sep 16, 2008 3:54 pm

Re: Create macro to insert index entry

Postby sh1 » Tue Feb 24, 2009 12:08 am

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
sh1
 
Posts: 14
Joined: Sun Feb 22, 2009 1:19 am

Re: Create macro to insert index entry

Postby FJCC » Tue Feb 24, 2009 12:48 am

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   Expand viewCollapse view
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.
AOO 3.4 or 4.1 on MS Windows XP ( before 2013-08-03) or Windows 7
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 5840
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Create macro to insert index entry

Postby sh1 » Tue Feb 24, 2009 2:09 am

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
sh1
 
Posts: 14
Joined: Sun Feb 22, 2009 1:19 am

Re: Create macro to insert index entry

Postby turtle47 » Tue Feb 24, 2009 7:33 am

@ sh1 Sorry for the mistake? :oops:

@ FJCC Thanks for correction. :idea:
OOo 3.2.X on Ms Windows 7
turtle47
 
Posts: 31
Joined: Tue Sep 16, 2008 3:54 pm

Re: [Solved] Create macro to insert index entry

Postby timur » Wed Apr 15, 2009 1:03 am

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
timur
 
Posts: 5
Joined: Wed Apr 15, 2009 12:57 am

Re: [Solved] Create macro to insert index entry

Postby FJCC » Wed Apr 15, 2009 4:24 am

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.
AOO 3.4 or 4.1 on MS Windows XP ( before 2013-08-03) or Windows 7
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 5840
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: [Solved] Create macro to insert index entry

Postby timur » Wed Apr 15, 2009 9:47 pm

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
timur
 
Posts: 5
Joined: Wed Apr 15, 2009 12:57 am

Re: [Solved] Create macro to insert index entry

Postby Hagar Delest » Wed Apr 15, 2009 10:11 pm

Just inserting it (even if no entries yet) perhaps?
AOO 4.1.3 on Xubuntu 17.04, (Zesty Zapus) and on Windows 7 (with winPenPack port).
User avatar
Hagar Delest
Moderator
 
Posts: 27467
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: [Solved] Create macro to insert index entry

Postby timur » Wed Apr 15, 2009 10:23 pm

I'm sorry, I still don't understand what you're talking about. Insert what where?
OOo 3.0.X on Fedora other
timur
 
Posts: 5
Joined: Wed Apr 15, 2009 12:57 am

Re: [Solved] Create macro to insert index entry

Postby Hagar Delest » Wed Apr 15, 2009 10:42 pm

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.
AOO 4.1.3 on Xubuntu 17.04, (Zesty Zapus) and on Windows 7 (with winPenPack port).
User avatar
Hagar Delest
Moderator
 
Posts: 27467
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: [Solved] Create macro to insert index entry

Postby FJCC » Thu Apr 16, 2009 1:02 am

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 274 times
AOO 3.4 or 4.1 on MS Windows XP ( before 2013-08-03) or Windows 7
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 5840
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: [Solved] Create macro to insert index entry

Postby Hagar Delest » Thu Apr 16, 2009 8:07 am

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.
AOO 4.1.3 on Xubuntu 17.04, (Zesty Zapus) and on Windows 7 (with winPenPack port).
User avatar
Hagar Delest
Moderator
 
Posts: 27467
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: [Solved] Create macro to insert index entry

Postby FJCC » Thu Apr 16, 2009 2:46 pm

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.
AOO 3.4 or 4.1 on MS Windows XP ( before 2013-08-03) or Windows 7
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 5840
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: [Solved] Create macro to insert index entry

Postby smok2212 » Wed Sep 30, 2009 10:20 pm

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
smok2212
 
Posts: 18
Joined: Thu Mar 19, 2009 2:34 am

Re: [Solved] Create macro to insert index entry

Postby RoryOF » Wed Sep 30, 2009 10:26 pm

Position cursor where you want the index. Insert / Indexes and Table and select the drop down for Index.
Apache OpenOffice 4.1.3 on Xubuntu 16.04 (mostly 64 bit version) and infrequently on Win2K/XP
14 October 2016 was Pooh's 90th birthday
User avatar
RoryOF
Moderator
 
Posts: 24183
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: [Solved] Create macro to insert index entry

Postby smok2212 » Wed Sep 30, 2009 11:18 pm

RoryOF, I want to do that using OOO Basic language, not manually.
OOo 3.0.X on Ms Windows XP
smok2212
 
Posts: 18
Joined: Thu Mar 19, 2009 2:34 am

Re: [Solved] Create macro to insert index entry

Postby gorthmog » Thu May 15, 2014 6:39 am

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   Expand viewCollapse view
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
gorthmog
 
Posts: 9
Joined: Sun Apr 20, 2014 5:09 am

Re: [Solved] Create macro to insert index entry

Postby stas » Tue Apr 18, 2017 8:15 am

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   Expand viewCollapse view
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
stas
 
Posts: 1
Joined: Tue Apr 18, 2017 8:04 am


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 3 guests