Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WORD200T, macro, using a variable rather then string 1

Status
Not open for further replies.

lupidol

Programmer
Apr 23, 2008
125
IL
Hi evereyone,
I try to add a macro process into my Word2007 to add a string to the Index.
I select "abcd" (a string in my word sheet) select "View" from the ribbon and "macroes" from the "view" ribbon. I select "record a macro" and give the new macro a name and OK. Starting to record the macro i select "reference" from the ribbon and "Add index" . At the "Value" text box "abcd" is already populated so i click OK and go back to "view", "Macroes" and select "Stop recording". A new value ("abcd") is added to the "Index" list.
Now i selct "efgh" from my "Word" sheet and press the macro button at the "Quick Access Toolbar" but rather then getting a new "efgh" value, i get "abcd" again.
How can i change a string with variable ?
Thanks.
 
What is the VBA code generated by the macro recorder ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi PHV,
This is the code:
Code:
Sub mymacro()
'
' mymacro Macro
'
'
    Selection.MoveLeft Unit:=wdCharacter, Count:=4, Extend:=wdExtend
    ActiveWindow.ActivePane.View.ShowAll = True
    ActiveDocument.Indexes.MarkEntry Range:=Selection.Range, Entry:="abcd", _
        EntryAutoText:="abcd", CrossReference:="", CrossReferenceAutoText:="", _
        BookmarkName:="", Bold:=False, Italic:=False
    ActiveWindow.ActivePane.View.ShowAll = Not ActiveWindow.ActivePane.View. _
        ShowAll
End Sub
[code/]
Thanks
 
Hi lupidol,

One of the limitations of the macro recorder is that it records exactly the inputs you give it, thus giving you some fairly inflexible code.

There are two things you might benefit from changing:
1. the allowable selection length; and
2. the way the index entry is determined.

The easiest way to achieve both aims is to select your desired index entry's text before running the following modified version of your code (note the relatively minor changes):
Code:
Sub mymacro()
ActiveWindow.ActivePane.View.ShowAll = True
ActiveDocument.Indexes.MarkEntry Range:=Selection.Range, Entry:=Selection.Range.Text, _
  EntryAutoText:=Selection.Range.Text, CrossReference:="", CrossReferenceAutoText:="", _
  BookmarkName:="", Bold:=False, Italic:=False
ActiveWindow.ActivePane.View.ShowAll = False
End Sub
Cheers

[MS MVP - Word]
 
Hi macropod,
The code you sent solved the problem. The problem of understandig it is a different story.
I was dealing with "Word" and now i find myself coping with VBA which i never intended to learn but i'm afraid i'll have to..
Thanks a lot !

 
Hi lupidol,

When trying to understand vba, it's often useful to select an expression you want to know more about in the vba and press F1. That'll open up the vba help file's entry on that expression. The accompanying text and (often) examples can be quite instructive for the beginner.

Another method of finding out more about a given expression (eg ActiveWindow.) is to insert a tick mark before the period (to comment out the rest of that line), then reposition the cursor at the end of the expression and input '.'. The vbe will usually respond by listing all of the associated mehtods/properties. You can often do something similar with expressions followed by ':=" and '('.

Cheers

[MS MVP - Word]
 
Thanks macropod,
I'll start doing as you suggest
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top