vba - 在 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”。但是文本中有一个明确的包含公式的字段......我怎样才能解决这个字段来改变它的值?最好的问候,安德烈亚斯
解决方案
推荐阅读
- laravel - Eloquent - 检测多列重复数据
- python - Flask - 将表单输入传递给 url_for
- angular - Angular 7 - 服务器端渲染
- firebase - 在 Flutter(并使用 Firebase 实时数据库)中,我如何获取我已注册的所有用户的快照?
- azure-devops - 如何使用 REST Api 确定 Azure DevOps 发布管道何时结束?
- apache - 容器运行超出内存限制 - 收到信号 15:SIGTERM
- c - 对我的 Atmega644 MCU 进行编程时。为什么 PORTD |= 0b00100000 有效,但 PORTD |= (PD5 <<1) 无效?
- sql - 如何在sql中创建一个带参数的视图
- c - 我的代码不返回输出。它只返回退出代码
- java - 如何在 Evosuite 测试生成时获得更多(所有)极端案例?