OK. I tested the macro on a small index I created and the result was fine.
The character style was applied to the desired text only. I cannot tell what
goes wrong with your index.
Did you define the character style as described in Suzanne Barnhill's
article? (the link in my first post).
Have you checked that you do not have the style Index 1 applied to text
before the index?
Did you make sure that field codes were not shown when running the macro?
In order to have the macro skip index styles other than Index 1, try to
replace the code line:
If oPara.Range.Text = Chr(12) Then
with
If oPara.Range.Text = Chr(12) Or oPara.Style <> "Index 1" Then
If nothing of the above helps, you can try to e-mail the document to me and
I will have a look. You can find the e-mail address in my profile.
--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
"Nannie" wrote:
> Lene,
> I installed your macro and, unfortunately, it did not solve the problem. It
> changed all of the index text to HeaderCharStyle. I still get too much
> information in the header (as before) and lose the formatting of level 2
> indices (indentation) and two column formatting.
> I DO appreciate your help, though!
> --
> Nannie
>
>
> "Lene Fredborg" wrote:
>
> > Yes, you will need to apply the character style as the last thing after you
> > have finished the editing and updated the index. If this is not an option,
> > you could use a macro to update the index and apply the character style. See
> > below.
> >
> > Preconditions:
> > 1. You have defined a character style named "HeaderCharStyle" (change the
> > name in the macro if you want another style name).
> > 2. Your header contains a StyleRef field with the code:
> > { StyleRef "HeaderCharStyle" }
> >
> >
> > The macro below first updates the index (and thereby removes any previously
> > applied character style). Then the macro iterates through all paragraphs in
> > the index. If one or more commas are found in the paragraph, the macro will
> > find the text until the first comma and apply the HeaderCharStyle. If no
> > commas are found, HeaderCharStyle will be applied to the entire paragraph
> > (minus the paragraph mark). The StyleRef field in the header should
> > automatically be updated.
> >
> >
> > Sub ApplyCharacterStyleToIndex()
> >
> > Dim oPara As Paragraph
> > Dim oRange As Range
> >
> > If ActiveDocument.Indexes.Count >= 1 Then
> > 'Update index
> > ActiveDocument.Indexes(1).Update
> > For Each oPara In ActiveDocument.Indexes(1).Range.Paragraphs
> > 'The first item may be a section break that starts the index
> > 'in that case, skip it - Chr(12)
> > If oPara.Range.Text = Chr(12) Then
> > GoTo Skip
> > End If
> > 'Get the text in the paragraph without the paragraph mark
> > Set oRange = oPara.Range
> > With oRange
> > .End = .End - 1
> > 'If comma in text, reduce until no comma
> > Do Until InStr(1, .Text, ",") = 0
> > .End = .End - 1
> > Loop
> > 'Apply character style to text
> > 'Replace "HeaderCharStyle" by the style name you defined
> > .Style = "HeaderCharStyle"
> > End With
> > Skip:
> > Next oPara
> > End If
> > 'Clean up
> > Set oRange = Nothing
> > End Sub
> >
> > For help on installing macros, see:
> > http://www.gmayor.com/installing_macro.htm
> >
> > --
> > Regards
> > Lene Fredborg
> > DocTools - Denmark
> > www.thedoctools.com
> > Document automation - add-ins, macros and templates for Microsoft Word
> >
> >
> > "Nannie" wrote:
> >
> > > I tried assigning a character style to just the surname but as soon as the
> > > index is re-generated, those assigned character styles are lost. But thanks
> > > for replying!!
> > > --
> > > Nannie
> > >
> > >
> > > "Lene Fredborg" wrote:
> > >
> > > > See the following article for tips on how you can accomplish what you want by
> > > > using a character style:
> > > > http://sbarnhill.mvps.org/WordFAQs/StyleRef.htm
> > > >
> > > > --
> > > > Regards
> > > > Lene Fredborg
> > > > DocTools - Denmark
> > > > www.thedoctools.com
> > > > Document automation - add-ins, macros and templates for Microsoft Word
> > > >
> > > >
> > > > "Nannie" wrote:
> > > >
> > > > > I have a generated index and used StyleRef to build running headers
> > > > > successfully using Index1. However, the header includes too much information.
> > > > > For example: Adams, George F of Company C 14, 25, 99. I'd like to truncate
> > > > > it to whatever comes before the comma. How can I do this?
> > > > > --
> > > > > Nannie