Results 1 to 15 of 15
  1. #1
    New Lounger
    Join Date
    Feb 2002
    Location
    Sydney, Australia
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Find text in Headers/Footers (Word 97, 2K, XP)

    If I search manually (in Word97) for text, with All selected, the search goes through the text, then through the headers and footers, but if I use a VBA macro, it goes through the "Story" only, without opening the headers and footers. Does anybody know how to make my macro search the headers and footers as well?

    (I love STYLEREF, but in Wd2K, text in Unicode Hebrew doesn't display properly if it comes out of a STYLEREF, so I want to be able to harden the STYLEREFs in a document, once the source has been set.)

    Here's my code, that handles the fields in the main document nicely, but doesn't look in the headers or footers. Any ideas?

    Sub ReplaceStyleRefs()
    ' Get the head word text,
    ' then for each STYLEREF that refers to it,
    ' replace the field code with the text in the clipboard
    '
    Dim Found As Boolean
    Dim FldCode As String

    ' If this document is a template,
    ' do nothing
    '
    If ActiveDocument.Type = wdTypeTemplate Then
    MsgBox _
    Title:="Macro to replace STYLEREF fields", _
    Prompt:="Sorry, the active document is a template, so I won't change" & _
    vbCr & "the STYLEREF fields into the Head Word."
    Exit Sub
    End If

    Selection.HomeKey Unit:=wdStory
    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Head Word")
    With Selection.Find
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Found = Selection.Find.Execute

    If Found = False Then
    MsgBox "Sorry, no text in Head Word style found."
    Exit Sub
    End If

    Selection.Copy
    Selection.Start = Selection.End
    ActiveWindow.View.ShowFieldCodes = True

    ' Go through the document, replacing StyleRef fields as appropriate.
    '
    ReplaceStyleRefInternal

    ActiveWindow.View.ShowFieldCodes = False
    End Sub
    Sub ReplaceStyleRefInternal()

    Do
    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = "^d"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Found = Selection.Find.Execute

    If Found = False Then
    Exit Do
    End If

    FldCode = Selection.Fields(1).Code
    If InStr(1, FldCode, "STYLEREF", 1) Then
    If InStr(1, FldCode, "Head Word", 1) Then
    Selection.PasteSpecial Link:=False, DataType:=wdPasteText, _
    Placement:=wdInLine, DisplayAsIcon:=False
    End If
    End If
    Loop While Found

    End Sub

  2. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Find text in Headers/Footers (Word 97, 2K, XP)

    If you want to search everything in the document then you should use something like...

    For each rngStory in ActiveDocument.StoryRanges

    rngStory.Find.ClearFormatting
    <font color=448800>' Put the rest of your find and replace code here.</font color=448800>

    Next rngStory

    This will include all headers, footers, text boxes, comments etc.

    If you only want to look at the Main document and headers/footers then use a Case statement like
    Select Case rngStory.StoryType
    Case wdMainTextStory
    <font color=448800>' Code to execute in the main document goes here</font color=448800>

    Case wdEvenPagesFooterStory, wdEvenPagesHeaderStory, wdFirstPageFooterStory, wdFirstPageHeaderStory, wdPrimaryFooterStory, wdPrimaryHeaderStory
    <font color=448800>' Code to execute in the headers and footers goes here</font color=448800>

    Case wdCommentsStory, wdEndnotesStory, wdFootnotesStory, wdTextFrameStory
    <font color=448800>' Code to execute in the rest of the document goes here</font color=448800>

    End Select

    StuartR

  3. #3
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: Find text in Headers/Footers (Word 97, 2K, XP)

    Hi Njimi,

    Try something based on:

    Sub UpdateAllFields()
    Dim Sctn As Section
    Dim HdFt As HeaderFooter
    ' ActiveDocument.Fields.Update ' replace this with your code
    For Each Sctn In ActiveDocument.Sections
    For Each HdFt In Sctn.Footers
    If Not HdFt.LinkToPrevious Then HdFt.Range.Fields.Update ' put your code after the "then"
    Next
    For Each HdFt In Sctn.Headers
    If Not HdFt.LinkToPrevious Then HdFt.Range.Fields.Update ' put your code after the "then"
    Next
    Next
    End Sub

    The above simply updates the Header & Footer fields. You can modify the code where indicated to call your styleref module, or incorporate it directly.

    Cheers
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  4. #4
    New Lounger
    Join Date
    Feb 2002
    Location
    Sydney, Australia
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find text in Headers/Footers (Word 97, 2K, XP)

    Thanks, Stuart and Macropod, for your instant answers!

    Stuart, I tried your suggestion, as it is more general, and I find that that it steps through all the stories OK, but doesn't actually find anything in the other stories, even though there are plenty of fields there to find.

    I put MsgBox rngStory in the loop, and it displays all the field codes nicely there, but the header windows don't open for searching the way they do if I do View->Headers or run a Find manually. Here's what comes after the ClearFormatting you suggested:

    ' Go through all parts of the document, replacing StyleRef fields as appropriate.
    '
    For Each rngStory In ActiveDocument.StoryRanges
    MsgBox rngStory

    ' Put the rest of your find and replace code here.
    Do

    rngStory.Find.ClearFormatting
    With Selection.Find
    .Text = "^d"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False

    etc.

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Find text in Headers/Footers (Word 97, 2K, XP)

    According to online help, the story ranges (StoryType) are: "wdCommentsStory, wdEndnotesStory, wdEvenPagesFooterStory, wdEvenPagesHeaderStory, wdFirstPageFooterStory, wdFirstPageHeaderStory, wdFootnotesStory, wdMainTextStory, wdPrimaryFooterStory, wdPrimaryHeaderStory, or wdTextFrameStory."

    Of these, as far as I can tell, there is only one wdMainTextStory, wdFootnotesStory, wdEndnotesStory, and wdCommentsStory for the entire document, regardless of the number or settings for the sections of that document. However, wdPrimaryHeaderStory, wdPrimaryFooterStory, wdFirstPageHeaderStory, wdFirstPageFooterStory, wdEvenPagesHeaderStory, and wdEvenPagesFooterStory have the potential to be different in every section. When you access these stories from the ActiveDocument object, you are accessing only the ones that apply to the first section. (If all headers and footers are linked, it covers them all, but if they're different, it doesn't.) As for wdTextFrameStory, I have no idea.

    So... that's why the section-by-section code also was suggested, to deal with the headers and footers.

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

  7. #7
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Find text in Headers/Footers (Word 97, 2K, XP)

    Your problem is the line
    With Selection.Find

    Which should be
    With rngStory.Find

    There are two different ways of using the Find object. If you use it with Selection.Find then it works a bit like the interactive find, moving your selection and leaving the find text in the dialog box for the next interactive use. If you use Find with a Range object then it doesn't change the selection, just finds the range you have asked for.

    StuartR

  8. #8
    New Lounger
    Join Date
    Feb 2002
    Location
    Sydney, Australia
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find text in Headers/Footers (Word 97, 2K, XP)

    Thanks, everyone, for your suggestions. I wasn't able to make the range.find approach work, because I need to search for a field, and then check that it is a STYLEREF and that the style referenced is "Head Word".

    But it now works, but looking through each story, and handles all the text boxes and headers successfully.

    The line:
    myStoryRange.Select
    I adapted from Steven Roman's book "Writing Word Macros", published by O'Reilly, in the nutshell series, from his example of searching a range (pp. 257-260)

    With that modification I was able to keep the same internal search subroutine
    ReplaceStyleRefsInternal
    to search and decide about the fields in each range.

    My code is attached.

    Jim
    Attached Files Attached Files

  9. #9
    3 Star Lounger rcbjr2's Avatar
    Join Date
    Jan 2001
    Location
    Matthews, NC
    Posts
    279
    Thanks
    6
    Thanked 1 Time in 1 Post

    Re: Find text in Headers/Footers (Word 97, 2K, XP)

    I am having a similar problem trying to set up a macro to search and replace something everywhere in a document, through all stories and all sections. I am using Word 2000 on a Windows 2000 system. I looked at the article on the MVP.ORG web site you mentioned and have created the following:

    Sub ClearDVFormatting()

    Dim myStoryRange As Range

    For Each myStoryRange In ActiveDocument.StoryRanges
    With myStoryRange.Find
    .Style = ActiveDocument.Styles("DeltaView Change Number")
    .Text = "^?" 'Searches for chars with Change Number style, then
    .Replacement.Text = "" 'replaces them with nothing
    .Wrap = wdFindContinue
    .Execute Replace:=wdReplaceAll
    End With
    Do While Not (myStoryRange.NextStoryRange Is Nothing)
    Set myStoryRange = myStoryRange.NextStoryRange
    With myStoryRange.Find
    .Style = ActiveDocument.Styles("DeltaView Change Number")
    .Text = "^?" 'Searches for chars with Change Number style, then
    .Replacement.Text = "" 'replaces them with nothing
    .Wrap = wdFindContinue
    .Execute Replace:=wdReplaceAll
    End With
    Loop
    Next myStoryRange

    End Sub

    This is virtually identical to the code from that article, except for the addition of the .style line, but it doesn't seem to work. I've stepped through the macro and it seems to get itself into an infinite loop. The first With seems to work and replaces all instances in the main body of the document. However, when the macro gets into the Do While Not loop, the macro seems to go into an infinite loop and repeats the Do While loop over and over. If I run the macro from within Word and not the VBA Editor, Word seems to close and the VBA Editor appears. I have to shut everything down from the Task List on Ctrl-Alt-Del.

    Is the problem that maybe it can't find what it's looking for? Would that create an infinite loop?

    By the way, is this code designed to work with all stories AND ALL sections? I've been reviewing a lot of posts on the Lounge today about updating fields and searching and replacing in stories and sections, and I'm not quite sure yet what is what and how to handle it all.

    Does anybody have any ideas??

    Thx.

    -Rich Belthoff

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find text in Headers/Footers (Word 97, 2K, XP)

    I would use an empty string "" as search text instead of ^?, and use .ClearFormatting and .Format = True at the beginning of the With ... End With blocks. The following works for me (with a style used in the document):
    <code>
    Sub ClearDVFormatting()
    Dim myStoryRange As Range

    For Each myStoryRange In ActiveDocument.StoryRanges
    With myStoryRange.Find
    .ClearFormatting
    .Format = True
    .Style = ActiveDocument.Styles("DeltaView Change Number")
    .Text = "" 'Searches for text with Change Number style, then
    .Replacement.Text = "" 'replaces them with nothing
    .Wrap = wdFindContinue
    .Execute Replace:=wdReplaceAll
    End With
    Do While Not (myStoryRange.NextStoryRange Is Nothing)
    Set myStoryRange = myStoryRange.NextStoryRange
    With myStoryRange.Find
    .ClearFormatting
    .Format = True
    .Style = ActiveDocument.Styles("DeltaView Change Number")
    .Text = "" 'Searches for text with Change Number style, then
    .Replacement.Text = "" 'replaces them with nothing
    .Wrap = wdFindContinue
    .Execute Replace:=wdReplaceAll
    End With
    Loop
    Next myStoryRange

    Set myStoryRange = Nothing
    End Sub</code>

  11. #11
    3 Star Lounger rcbjr2's Avatar
    Join Date
    Jan 2001
    Location
    Matthews, NC
    Posts
    279
    Thanks
    6
    Thanked 1 Time in 1 Post

    Re: Find text in Headers/Footers (Word 97, 2K, XP)

    PERFECT!! Works great now.

    Thx.

    -Rich Belthoff

  12. #12
    3 Star Lounger rcbjr2's Avatar
    Join Date
    Jan 2001
    Location
    Matthews, NC
    Posts
    279
    Thanks
    6
    Thanked 1 Time in 1 Post

    Re: Find text in Headers/Footers (Word 97, 2K, XP)

    May I ask another question? I've modified the code to work with multiple styles and I call subroutines to do the searching and replacing. I'd like to display a progress bar as the macro runs through each subroutine. I'd like to do a % completion dialog notice to the user, but wasn't sure how to do it, so I used a MsgBox routine, but that always seems to require the user to click OK. Isn't there a way to configure MsgBox so that it doesn't ask for OK?? A snippet of my code is below. I would prefer to do a progress bar showing % completion if that can be done easy enough (I search on WOPR and on the web and couldn't find anything, but I may not have been searching for the appropriate terms). In any event, here is a snippet of my code showing the MsgBox routine I tried to use.

    For Each myStoryRange In ActiveDocument.StoryRanges
    Response MsgBox("Removing DeltaView Redline Markings.", vbExclamation)
    Call ChangeNumberStyle(myStoryRange)
    .....continues

    Thx.

    -Rich Belthoff

  13. #13
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Find text in Headers/Footers (Word 97, 2K, XP)

    Starting with Office 2000, you could display a non-modal UserForm that would be perfect for a progress bar. However, in Office 97, the only way to simulate that is to write characters to the Status bar at the bottom of the Word window, or to invoke a Windows control. For the latter, I think there were examples on the web 5 years ago; Google might still be able to track them down.

  14. #14
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find text in Headers/Footers (Word 97, 2K, XP)

    To expand on Jefferson's reply, you could do something like this:

    ' Clear status bar
    StatusBar = ""

    For Each myStoryRange In ActiveDocument.StoryRanges
    ' Update status bar
    StatusBar = StatusBar & "-"
    Call ChangeNumberStyle(myStoryRange)
    .....continues

    It does not display a percentage, but at least it gives the user some idea of how the macro progresses.

  15. #15
    3 Star Lounger rcbjr2's Avatar
    Join Date
    Jan 2001
    Location
    Matthews, NC
    Posts
    279
    Thanks
    6
    Thanked 1 Time in 1 Post

    Re: Find text in Headers/Footers (Word 97, 2K, XP)

    Thanks for the ideas. I did some further searching on the web and found the StatusBar idea several places, and also ran across a "Progress Bar" on MVP.ORG (if that's the correct site). It was also on one of the O'Reilly book sites as well. It looks neat, but might just be a bit too complicated for what I want to do. I'll probably use the StatusBar indicator.

    Thx.

    -Rich

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •