简体   繁体   English

Word VBA - 循环浏览带有条件的书签

[英]Word VBA - Looping through bookmarks with a condition

I am very new to VBA, so any help is much appreciated.我对 VBA 很陌生,所以非常感谢任何帮助。

I am trying to build a macro that accomplishes the following:我正在尝试构建一个实现以下功能的宏:

  1. When run a user form opens运行时打开用户表单

  2. The user form asks users to categorize different topics as "Applicable" or "Not Applicable"用户表单要求用户将不同的主题分类为“适用”或“不适用”

  3. Once each topic is categorized, the macro will move all "Not Applicable" topics to the "Not Applicable" section in the document对每个主题进行分类后,宏会将所有“不适用”主题移动到文档中的“不适用”部分

  4. Cateogrization would be "Applicable" by default默认情况下,分类为“适用”

Each topic is set as its own bookmark.每个主题都设置为自己的书签。 I'm stuck on the For each / next portion of the code.我被困在代码的 For each / next 部分。 I have this so far:到目前为止我有这个:

Sub CleanUp()    
    Dim doc As Document
    Dim book As Bookmark

    For Each book In ActiveDocument.Range.Bookmarks
        If book = "Applicable" Then
        Selection.GoTo what;= wdgotobookmark, which:= book
        Selection.Cut
        Selection.GoTo what:=wdGoToBookmark, Name:="PASTE_HERE"
        Selection.PasteAndFormat (wdFormatOriginalFormatting)
    Next book
end sub

Two initial questions:两个初步问题:

  1. How can I use the optionbutton to set a value for each topic?如何使用选项按钮为每个主题设置一个值? Can I even use a user form like this?我什至可以使用这样的用户表单吗? There's about 50 topics, so I wasn't sure if I need to set a variable for each topic.大约有 50 个主题,所以我不确定是否需要为每个主题设置一个变量。

     Private Sub OptionButton2_Click() Set book = "Applicable" End Sub
  2. If I do need to create a variable for each topic/bookmark, is it better to loop with for/next?如果我确实需要为每个主题/书签创建一个变量,使用 for/next 循环是否更好? That way I can set the variable name equal to the counter?这样我就可以将变量名设置为等于计数器? For example topic1, topic2, topic3, etc.?例如topic1、topic2、topic3等?

A better approach (IMHO) would be to use Document Variables whose state could be tested via IF fields in the document such that, depending on the test result, the content would be displayed in the "Applicable" or "Not Applicable" section, as appropriate.更好的方法(恕我直言)是使用文档变量,其状态可以通过文档中的 IF 字段进行测试,这样,根据测试结果,内容将显示在“适用”或“不适用”部分中,如合适的。

For example, suppose you create a Document Variable named 'Topic1' whose values can be 1 or 0. In the:例如,假设您创建了一个名为“Topic1”的文档变量,其值可以是 1 或 0。在:

  • "Applicable" section, you'd have an IF field coded along the lines of: “适用”部分,您将有一个 IF 字段编码为:

    {IF{DOCVARIABLE Topic1}= 1 "Content to show"} {IF{DOCVARIABLE Topic1}= 1“要显示的内容”}

  • "Not Applicable" section, you'd have an IF field coded along the lines of: “不适用”部分,您将有一个 IF 字段编码为:

    {IF{DOCVARIABLE Topic1}= 0 "Content to show"} {IF{DOCVARIABLE Topic1}= 0“要显示的内容”}

where "Content to show" is the same in each field.其中“要显示的内容”在每个字段中都相同。 To simplify processing, you might just create the fields in the "Applicable" section, then copy them to the "Not Applicable" section and change the 1s to 0s.为了简化处理,您可能只在“适用”部分创建字段,然后将它们复制到“不适用”部分并将 1 更改为 0。

If checkboxes are used to indicate whether a given topic is applicable, the macro code could then be as simple as:如果使用复选框来指示给定主题是否适用,则宏代码可以像这样简单:

Private Sub CommandButton1_Click()
Dim Ctrl As Control: Const CtrlType As String = "CheckBox"
For Each Ctrl In UserForm1.Controls
  If TypeName(Ctrl) = CtrlType Then
  With Ctrl
    If .Caption Like "Topic#*" Then
      ActiveDocument.Variables(.Name).Value = .Value ^ 2
    End If
  End With
  End If
Next
ActiveDocument.Fields.Update
End Sub

Note: The field brace pairs (ie '{ }') for the above examples are all created in the document itself, via Ctrl-F9 (Cmd-F9 on a Mac or, if you're using a laptop, you might need to use Ctrl-Fn-F9);注意:上述示例的字段大括号对(即“{}”)都是在文档本身中创建的,通过 Ctrl-F9(Mac 上的 Cmd-F9,或者,如果您使用的是笔记本电脑,您可能需要使用 Ctrl-Fn-F9); you can't simply type them or copy & paste them from this message.您不能简单地键入它们或从该消息中复制和粘贴它们。 Nor is it practical to add them via any of the standard Word dialogues.通过任何标准 Word 对话添加它们也不切实际。 The spaces represented in the field constructions are all required.字段结构中表示的空格都是必需的。

For more on the use of Document Variables, see: https://support.microsoft.com/en-us/help/306281/how-to-store-and-retrieve-variables-in-word-documents有关使用文档变量的更多信息,请参阅: https : //support.microsoft.com/en-us/help/306281/how-to-store-and-retrieve-variables-in-word-documents

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

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