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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Highlighting fields in Word headers

Status
Not open for further replies.

mmtraining

IS-IT--Management
Mar 18, 2002
104
0
0
DE
Hi, All,

I need to search through a document including the headers for fields whose names have been changed for some reason (up until the changeover fom Office 97 to Office XP the fields were called Titel1, Titel2 and Titel3 and are now Title1, Title2 and Title3)

I have got this far:

Code:
Sub Felder_korrigieren()

    Dim oFeld As Field

    ActiveWindow.View.ShowFieldCodes = True
       
        For Each oFeld In ActiveDocument.Fields
            oFeld.Select
            Selection.Find.Execute FindText:="Title", ReplaceWith:="Titel", _
            Replace:=wdReplaceOne
        Next oFeld
        
        Selection.WholeStory
        Selection.Fields.Update

    ActiveWindow.View.ShowFieldCodes = False
    
End Sub

This deals nicely with the main document, now I need to hop into the headers (there is one or more) and deal with the fields up there. Any ideas?

Should anyone have an idea how to do the whole thing more elegantly, please tell me how. I have the sneaking feeling I could replace the fields Title1, Title3, Title3, with the "real" ones.

Thanx a lot in advance,

Carol, Berlin :)
 
Hi Carol,

No-one else has jumped in on this one so I'll try. I'm not very good with fields but a quick overview of document components might help.

Documents consist of a number of stories, main text, first page header, footnotes etc. Stories do not exist by default - only if you have relevant content in your document.

ActiveDocument by default works with the wdMainTextStory. To work with a different one you must explicitly specify it by using something like, in your case ..

Code:
ActiveDocument.StoryRanges(wdEvenPagesHeaderStory).Fields

It is not immediately obvious how to loop through all the stories in a document. There is a NextStoryRange property but it works within subsets of the possible stories. What you should, however, be able to do is add an extra For .. Next loop to your code, giving something like

Code:
Dim myStory

For Each myStory In ActiveDocument.StoryRanges

    ActiveWindow.View.ShowFieldCodes = True
       
        For Each oFeld In
Code:
myStory
Code:
.Fields
            oFeld.Select
            Selection.Find.Execute FindText:="Title", ReplaceWith:="Titel", _
            Replace:=wdReplaceOne
        Next oFeld
        
        Selection.WholeStory
        Selection.Fields.Update

    ActiveWindow.View.ShowFieldCodes = False

Next myStory

I haven’t tested this so you make have to tweak it, but it should get you going in the right direction.

Enjoy,
Tony
 
Thanx a lot, Tony,

Although I had a mistake in my programming, I got it to work, having used your ideas and corrected my bits and pieces.

All the best

Carol, Berlin :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top