Results 1 to 6 of 6
  1. #1
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    Portland, Maine, USA
    Posts
    296
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Finding fields (Word 2002)

    As part of post-clean up in some of my templates, I loop through a document, check each field to see if it is a custom document property (wdFieldDocProperty). If it is, I unlink it, then delete all the custom properties. This is working well for the body of the document. See code at the bottom of this post.

    Is there an easy way to search through the fields that may be in headers/footers? - Or - Do I have to count sections then check each section for different first page then go into each header and footer? Within my templates, I know where the fields are, but I want to extend this to documents/templates other may have created.

    Dim i As Integer
    Dim intX As Integer
    Dim proDoc As DocumentProperty

    intX = ActiveDocument.Fields.Count

    For i = intX To 1 Step -1
    If ActiveDocument.Fields(i).Type = wdFieldDocProperty Then
    ActiveDocument.Fields(i).Unlink
    End If
    Next
    For Each proDoc In ActiveDocument.CustomDocumentProperties
    proDoc.Delete
    Next

  2. #2
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Finding fields (Word 2002)

    Darryl,

    This is a chunk of code I use to loop through all the fields in a document, headers and footers included, to update them. Instead of updating them, you can unlink or delete them as needed.

    Dim rngStory As Range ' Range Object for Looping through Stories
    Dim doc As Document ' Pointer to Active Document
    Dim wnd As Window ' Pointer to Document's Window
    Dim lngMain As Long ' Main Pane Type Holder
    Dim lngSplit As Long ' Split Type Holder
    Dim lngActPane As Long ' ActivePane Number

    ' Set Objects
    Set doc = ActiveDocument
    Set wnd = ActiveDocument.ActiveWindow

    ' get Active Pane Number
    lngActPane = wnd.ActivePane.Index

    ' Hold View Type of Main pane
    lngMain = wnd.Panes(1).View.Type

    ' Hold SplitSpecial
    lngSplit = wnd.View.SplitSpecial

    ' Get Rid of any split
    wnd.View.SplitSpecial = wdPaneNone

    ' Set View to Normal
    wnd.View.Type = wdNormalView

    ' Loop through each story in doc to update
    For Each rngStory In doc.StoryRanges
    If rngStory.StoryType = wdCommentsStory Then
    Application.DisplayAlerts = wdAlertsNone
    ' Update fields
    rngStory.Fields.Update
    Application.DisplayAlerts = wdAlertsAll
    Else
    ' Update fields
    rngStory.Fields.Update
    End If
    Next

    'More Code Snipped

    ' Return Split to original state
    wnd.View.SplitSpecial = lngSplit

    ' Return main pane to original state
    wnd.Panes(1).View.Type = lngMain

    ' Active proper pane
    wnd.Panes(lngActPane).Activate

    ' Close and release all pointers
    Set wnd = Nothing
    Set doc = Nothing

    For some reason, that I haven't figured out, you need to be in Normal view for this to work properly, that's what the bit of code before the loop is for. And the bit after is to restore the views to the way they were originally.
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

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

    Re: Finding fields (Word 2002)

    An alternative approach, if you only want to process fields that are in headers and footers, is...

    Sub DoHeaderAndFooterFields()
    Dim secSection As Section
    Dim hfHeader As headerfooter
    Dim hfFooter As headerfooter
    Dim fldField as Field

    For Each secSection In ActiveDocument.Sections
    For Each hfHeader In secSection.Headers
    If Not hfHeader.LinkToPrevious Then
    For Each fldField in hfHeader.Fields
    If fldField.Type = wdFieldDocProperty Then fldField.Unlink
    Next fldField
    End If
    Next hfHeader
    For Each hfFooter In secSection.Footers
    If Not hfFooter.LinkToPrevious Then
    For Each fldField in hfFooter.Fields
    If fldField.Type = wdFieldDocProperty Then fldField.Unlink
    Next fldField
    End If
    Next hfFooter
    Next secSection
    End Sub

    StuartR

  4. #4
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    Portland, Maine, USA
    Posts
    296
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Finding fields (Word 2002)

    Thank you both for the input! Here is my final result. It looks through fields in the main doc checking for fields. It updates then unlinks a field if it is a custom document property. It then looks though headers and footers doing the same. (Thank you Stuart. A couple of minor changes but otherwise calls your code.)

    Sub RemoveCustomProps()
    '
    '
    '

    Dim i As Integer
    Dim intX As Integer
    Dim proDoc As DocumentProperty

    intX = ActiveDocument.Fields.Count

    For i = intX To 1 Step -1

    If ActiveDocument.Fields(i).Type = wdFieldDocProperty Then
    ActiveDocument.Fields(i).Update
    ActiveDocument.Fields(i).Unlink
    End If
    Next i


    DoHeaderAndFooterFields

    For Each proDoc In ActiveDocument.CustomDocumentProperties
    proDoc.Delete
    Next

    ActiveWindow.View.Type = wdPrintView

    End Sub

    Sub DoHeaderAndFooterFields()
    Dim secSection As Section
    Dim hfHeader As HeaderFooter
    Dim hfFooter As HeaderFooter
    Dim fldField As Field

    For Each secSection In ActiveDocument.Sections
    For Each hfHeader In secSection.Headers
    If Not hfHeader.LinkToPrevious Then
    For Each fldField In hfHeader.Range.Fields
    If fldField.Type = wdFieldDocProperty Then fldField.Update
    If fldField.Type = wdFieldDocProperty Then fldField.Unlink
    Next fldField
    End If
    Next hfHeader
    For Each hfFooter In secSection.Footers
    If Not hfFooter.LinkToPrevious Then
    For Each fldField In hfFooter.Range.Fields
    If fldField.Type = wdFieldDocProperty Then fldField.Unlink
    Next fldField
    End If
    Next hfFooter
    Next secSection
    End Sub

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

    Re: Finding fields (Word 2002)

    One stylistic comment and one correction...

    Instead of
    If fldField.Type = wdFieldDocProperty Then fldField.Update
    If fldField.Type = wdFieldDocProperty Then fldField.Unlink

    It would be better to have
    If fldField.Type = wdFieldDocProperty Then
    fldField.Update
    fldField.Unlink
    End If

    You probably meant to update the field in the footer (using the same code as above) before you unlink it

    StuartR

  6. #6
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    Portland, Maine, USA
    Posts
    296
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Finding fields (Word 2002)

    I noticed both of those things after I posted. Thank you for pointing them out!

Posting Permissions

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