Michal Janošec
Michal Janošec

Reputation: 81

VBA Word macro not working as expected with field results in document

I have a word document (report) and in that document, I'm importing many text files with fields like this:

{INCLUDETEXT "C:\\PATH\\TOXMLFILES\\Request.xml" \*CHARFORMAT}

Also I'm updating all those fields with a macro on opening the document...

Sub AutoOpen()
With Options
    .UpdateFieldsAtPrint = True
    .UpdateLinksAtPrint = True
End With
ActiveDocument.Fields.Update
End Sub

Now I need to highlight the text of those imported XMLs (in the IncludeText fields) between <faultstring></faultstring> tags

Here is code I got here on stackoverflow for highlighting text (making it bold)

Sub BoldBetweenQuotes()
    ' base for a quotes finding macro
    Dim blnSearchAgain As Boolean
    Dim blnFindStart As Boolean
    Dim blnFindEnd As Boolean
    Dim rngFind As word.Range
    Dim rngFindStart As word.Range
    Dim rngFindEnd As word.Range

    Set rngFind = ActiveDocument.content
    Set rngFindStart = rngFind.Duplicate
    Do
        ' set up find of first of quote pair
        With rngFindStart.Find
            .ClearFormatting
            .Text = "<faultstring>"
            .Replacement.Text = ""
            .Forward = True
            .wrap = wdFindStop
            blnFindStart = .Execute
        End With
        If blnFindStart Then
            rngFindStart.Collapse wdCollapseEnd
            Set rngFindEnd = rngFindStart.Duplicate
            rngFindEnd.Find.Text = "</faultstring>"
            blnFindEnd = rngFindEnd.Find.Execute
            If blnFindEnd Then
                rngFindStart.End = rngFindEnd.Start
                ' make it bold
                rngFindStart.Font.Bold = True
                rngFindStart.Start = rngFindEnd.End
                rngFindStart.End = rngFind.End
                blnSearchAgain = True
            Else
                blnSearchAgain = False
            End If
        Else
            blnSearchAgain = False
        End If
    Loop While blnSearchAgain = True
End Sub

Problem is, when I run the macro in my Word document (with the IncludeText fields) it keeps cycling and bolding just the first appearance of text between faultstring tags. When I run it in a new Word document with some random text and faultrstring tags it works well...

EDIT: It turns out the problem is due to the faultstring tags being inside the IncludeText fields. I need to turn the fields into static text after opening the document and updating the fields. How can I do that?

Upvotes: 1

Views: 1431

Answers (1)

Cindy Meister
Cindy Meister

Reputation: 25663

In order to convert dynamic field content to static text using Word's object model (such as VBA) the Fields.Unlink method is required. For the entire document:

ActiveDocument.Fields.Unlink

This is also possible for any given Range; to remove the fields in the last paragraph, for example:

ActiveDocument.Paragraphs.Last.Range.Fields.Unlink

In order to unlink only a certain type of field, loop the Fields collection, test the Field.Type and unlink accordingly. For example, for IncludeText:

Sub DeleteIncludeTextFields()
    Dim doc As word.Document

    Set doc = ActiveDocument
    Debug.Print DeleteFieldType(wdFieldIncludeText, doc)
End Sub

Function DeleteFieldType(fldType As word.WdFieldType, doc As word.Document) _
         As Long

    Dim fld As word.Field
    Dim counter As Long

    counter = 0
    For Each fld In doc.Fields
        If fld.Type = wdFieldIncludeText Then
            fld.Unlink
            counter = counter + 1
        End If
    Next

    DeleteFieldType = counter
End Function

Assuming you want to do this for all the fields in your document, after updating it:

Sub AutoOpen()
  With Options
    .UpdateFieldsAtPrint = True
    .UpdateLinksAtPrint = True
  End With
  ActiveDocument.Fields.Update
  ActiveDocument.Fields.Unlink
End Sub

Upvotes: 1

Related Questions