简体   繁体   English

MS Word VBA脚本可将书签添加到现有内容控件

[英]MS Word VBA script to add Bookmarks to Existing Content Controls

I have over 150 Content Control (CC) boxes within a document and was wondering if someone would be able to help me with VBA script to add bookmarks to them programmatically. 我在一个文档中有超过150个内容控制(CC)框,并且想知道是否有人可以通过VBA脚本帮助我以编程方式向他们添加书签。 The names for the new bookmarks would have to match the Title or Tags I've inserted for the CCs. 新书签的名称必须与我为CC插入的标题或标签匹配。 For example, if I have a CC in the document with the title 'TestResults' I'd like the bookmark called 'TestResults' to be inserted on that CC. 例如,如果我在文档中有一个标题为“ TestResults”的抄送,我希望将名为“ TestResults”的书签插入该抄送中。 I'm an absolute beginner to VB! 我绝对是VB的初学者!

So I guess I want the code to do the following: 所以我想我希望代码执行以下操作:

  1. Find all the CCs in the document and their titles 在文档及其标题中找到所有抄送
  2. Insert bookmarks matching the CC title. 插入与抄送标题匹配的书签。

This should work for you: 这应该为您工作:

Sub AddBookmarksAtCC()
    Dim ccobjA As ContentControl, i As Integer
    For i = 1 To ActiveDocument.ContentControls.Count
        Set ccobjA = ActiveDocument.ContentControls.Item(i)
        Debug.Print ccobjA.Title
        ActiveDocument.Bookmarks.Add ccobjA.Title, ActiveDocument.ContentControls.Item(i).Range
    Next i
End Sub

This procedure will count all Content Controls on the document, and loop though them all, adding a bookmark at each one. 此过程将对文档中的所有内容控件进行计数,并遍历它们,并在每个控件上添加一个书签。 As it is, the bookmark will be named the title of the content control. 照原样,书签将被命名为内容控件的标题。 If you want to make it the Tag, replace ccobjA.Title with ccobjA.Tag . 如果要使其成为Tag,请将ccobjA.Title替换为ccobjA.Tag Let me know if you are still having trouble. 让我知道您是否仍然遇到麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM