在word vba中域对象为Field对象,所有域的集合为Fields对象。
word 域可以在域代码和域结果之间切换显示。
域代码可以使用Field对象的Code属性来访问,它将返回一个Range对象,代表一个域代码{}字符之间的所有内容。
以下vba代码举例了如何输出文档中的所有域代码:
Sub QQ1722187970() Word.Application.ScreenUpdating = False Dim oField As Field Dim oDoc As Document Dim oRng As Range Set oDoc = Word.ActiveDocument With oDoc For Each oField In .Fields With oField Set oRng = .Code Debug.Print oRng.Text End With Next End With Word.Application.ScreenUpdating = True End Sub
域的结果可以使用Result属性,它也将返回一个Range对象,代表的是域代码的显示结果。
以下vba代码举例了如何输出文档中的所有域结果:
Sub QQ1722187970() Word.Application.ScreenUpdating = False Dim oField As Field Dim oDoc As Document Dim oRng As Range Set oDoc = Word.ActiveDocument With oDoc For Each oField In .Fields With oField Set oRng = .Result Debug.Print oRng.Text End With Next End With Word.Application.ScreenUpdating = True End Sub
为了不让域代码的结果自动更新,可以设置Locked属性为True,将域锁定。
以下vba代码举例了如何将文档中的所有域锁定,使其不自动更新:
Sub QQ1722187970() Word.Application.ScreenUpdating = False Dim oField As Field Dim oDoc As Document Dim oRng As Range Set oDoc = Word.ActiveDocument With oDoc For Each oField In .Fields With oField .Locked = True End With Next End With Word.Application.ScreenUpdating = True End Sub
在word文档中可以插入各种域,域可以有相应的域类型,访问Type属性可以返回域的类型,域类型可以详见域类型清单。
除了域类型,还可以通过Kind属性返回域的链接类型,域的链接类型有以下四种:
Name | Value | Description |
---|---|---|
wdFieldKindCold | 3 | A field that does not have a result, for example, an Index Entry (XE), Table of Contents Entry (TC), or Private field. |
wdFieldKindHot | 1 | A field that’s automatically updated each time it is displayed or each time the page is reformatted, but which can also be manually updated (for example, INCLUDEPICTURE or FORMDROPDOWN). |
wdFieldKindNone | 0 | An invalid field (for example, a pair of field characters with nothing inside). |
wdFieldKindWarm | 2 | A field that can be updated and has a result. This type includes fields that are automatically updated when the source changes and fields that can be manually updated (for example, DATE or INCLUDETEXT). |
如以下代码举例演示了如何删除所有的空域或者无效的域,也就是只有{}符号,里面没有其它内容的:
Sub QQ1722187970() Word.Application.ScreenUpdating = False Dim oField As Field Dim oDoc As Document Dim oRng As Range Set oDoc = Word.ActiveDocument With oDoc For Each oField In .Fields With oField If .Kind = wdFieldKindNone Then .Delete End If End With Next End With Word.Application.ScreenUpdating = True End Sub
在Field对象中有一个方法是最重要的方法,这个方法是Unlink方法。如果word文档中有很多域,现在想要把所有的域都替换为以域结果显示的静态文本,也就是不再使用域,就可以使用Unlink方法将所有域转换为不含域代码的结果文本。
如以下代码举例演示了如何将所有的SEQ域转换为静态的文本:
Sub QQ1722187970() Word.Application.ScreenUpdating = False Dim oField As Field Dim oDoc As Document Dim oRng As Range Set oDoc = Word.ActiveDocument With oDoc For Each oField In .Fields With oField If .Type = wdFieldSequence Then .Unlink End If End With Next End With Word.Application.ScreenUpdating = True End Sub
如果要更新具体的某个域结果,可以使用Update方法。
如果要一次性更新或者解除关联所有的域,可以使用Fields对象的Update方法和Unlink方法。
如以下代码举例演示了如何将文档中的所有域更新并转换为静态文本:
Sub QQ1722187970() Word.Application.ScreenUpdating = False Dim oField As Field Dim oDoc As Document Set oDoc = Word.ActiveDocument With oDoc .Fields.Update .Fields.Unlink End With Word.Application.ScreenUpdating = True End Sub
发表评论