简体   繁体   中英

Word VBA - Looping through bookmarks with a condition

I am very new to VBA, so any help is much appreciated.

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. 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.

     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? That way I can set the variable name equal to the counter? For example topic1, topic2, topic3, etc.?

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.

For example, suppose you create a Document Variable named 'Topic1' whose values can be 1 or 0. In the:

  • "Applicable" section, you'd have an IF field coded along the lines of:

    {IF{DOCVARIABLE Topic1}= 1 "Content to show"}

  • "Not Applicable" section, you'd have an IF field coded along the lines of:

    {IF{DOCVARIABLE Topic1}= 0 "Content to show"}

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.

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); 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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