• Finding fields (Word 2002)

    Author
    Topic
    #382488

    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

    Viewing 1 reply thread
    Author
    Replies
    • #648662

      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.

    • #648667

      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

      • #648767

        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

        • #648791

          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

    Viewing 1 reply thread
    Reply To: Finding fields (Word 2002)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: