简体   繁体   中英

Word VBA - Loop through “AND” to delete bookmarks

I am writing a macro in VBA Word that is copying data from an excel file and pasting the data in the Word file using predefined bookmarks. I have two different groups of bookmarks in Word and, based on the action to perform, the macro is first deleting the bookmarks not needed for that specific action and then pasting the data where the remaining bookmarks are.

The code so far works in both cases but, not being a proficient programmer, I had to write several lines for a specific task to work and I would like your help to "clean" this part of code, make it less "intricate" and also easier to amend in case future changes are needed. Below you will find the code that, as I said, works but it is not pretty.

Dim arr As Variant
Dim bookmark As Bookmarks
Dim i As Long

arr = Array("a", "b", "c")

For Each bookmark In ActiveDocument.Bookmarks
    If bookmark <> arr(0) And bookmark <> arr(1) _
    And bookmark <> arr(2) Then
    bookmark.Delete
    Else
    bookmark.Select 'this is just to give a "useless" alternative to vba
    End If
Next bookmark  

I only put 3 elements of the array here, while in reality there are many, but the idea is to automate the "AND" function and the scope is the following: if every single bookmark already present in the Word file is different than ANY element of the array "arr" then that bookmark has to be deleted, otherwise nothing happens to it. So, instead of using one AND function for every element of the array I would like to loop through the array so that every bookmark is compared to ALL the elements of the array and only if different is removed. I do not know if there is a way to loop the "AND" function somehow, but adding more elements to the array would mean add the same number of "AND".

Hope to have been exhaustiv and that you can help me.

Thanks a lot in advance!

Loop the array, and use a Boolean to keep track if found or not.

Dim arr As Variant
Dim bookmark As Bookmarks
Dim i As Long
Dim bln As Boolean

arr = Array("a", "b", "c")

For Each bookmark In ActiveDocument.Bookmarks
    bln = False
    For i = LBound(arr) To UBound(arr)
        If bookmark = arr(i) Then
            bln = True
            Exit For
        End If
    Next i
    If Not bln Then bookmark.Delete
Next bookmark

If you have just a few elements, instead o using "If" and a loop through the array you could just use a Select Case

Select bookmark
Case Is <> "a","b","c"
bookmark.Delete
Case Else
'Whatever code
End Select

But depending on the size of the array the loop solution could be better.

You could use Join() function to obtain a string from all bookmarks array values and then Instr() function to check for current bookmark to be there

Dim arr As Variant
Dim bookmark As Bookmark

arr = Array("a", "b", "c")

For Each bookmark In ActiveDocument.Bookmarks
    If Instr(Join(arr, "|"), bookmark) = 0 Then bookmark.Delete
Next bookmark  

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