如何用vba操作word域以及域代码?

您所在的位置:网站首页 word域的添加和修改 如何用vba操作word域以及域代码?

如何用vba操作word域以及域代码?

2024-06-21 01:57| 来源: 网络整理| 查看: 265

如何用vba操作word域以及域代码? 2018年6月11日 作者:水星Excel 13,839 次浏览 暂无评论

在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

 

其它相关文章推荐:如何使得word中的金额大写中的繁体萬字变成简体万字?如何用vba访问使用word的“行对象”?如何用vba为word文档中的所有表格自动插入题注和交叉引用?如何用vba在word文档的末尾或者开头插入内容?如何用vba将word文档按照手动分页符拆分?如何用vba获得word range对象所在的行的文本内容? 分类 :  VBA , Word , WORD VBA , Word域代码 标签 :  Fields , Locked , Unlink , Update , 关联 , 域 , 域代码 , 更新 , 静态文本        

声明: 本站文章均属原创,未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则视为侵权。如果想要每天都收到本站的技术文章推送,可以微信搜索公众号:水星Excel或者Excel_Office关注。其它事宜请加QQ:1722187970 或微信号:xycgenius 联系。 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3