首页 > 解决方案 > 在 MS Word 中使用公式识别/更改字段

问题描述

我有一个word文档,其中包含标题中的一些字段和文本中的一些字段。标题中的字段可以通过以下方式更改:

Dim oHF As HeaderFooter
    Dim i As Integer
    i = 1
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    Do Until i > ActiveDocument.Sections.Count
        Set oHF = ActiveDocument.Sections(i).Headers(wdHeaderFooterFirstPage)
        For Each fieldLoop In oHF.Range.Fields
            If fieldLoop.Code.Text = " NUMPAGES \* MERGEFORMAT \* CHARFORMAT " Then
                fieldLoop.Code.Text = " SECTIONPAGES \* MERGEFORMAT \* CHARFORMAT "
            End If
        Next fieldLoop
        Set oHF = ActiveDocument.Sections(i).Headers(wdHeaderFooterPrimary)
        For Each fieldLoop In oHF.Range.Fields
            If fieldLoop.Code.Text = " NUMPAGES \* MERGEFORMAT \* CHARFORMAT " Then
                fieldLoop.Code.Text = " SECTIONPAGES \* MERGEFORMAT \* CHARFORMAT "
            End If
        Next fieldLoop
        i = i + 1
    Loop

但是,如果我尝试通过以下方式更改“主”文档中的字段:

Dim fieldX As field
    For Each fieldX In ActiveDocument.Fields
        If CStr(fieldX.Code) Like "*NUMPAGES*" Then
            fieldx.Code.Text = " SECTIONPAGES \* MERGEFORMAT \* CHARFORMAT "
        End If
    Next fieldX

它不起作用。甚至 ActiveDocument.fields.count 给我一个“0”。但是文本中有一个明确的包含公式的字段......我怎样才能解决这个字段来改变它的值?最好的问候,安德烈亚斯

标签: vbams-word

解决方案


推荐阅读