简体   繁体   English

Word VBA-通过“ AND”循环删除书签

[英]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. 我在VBA Word中编写了一个宏,该宏正在从Excel文件复制数据,并使用预定义的书签将数据粘贴到Word文件中。 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. 我在Word中有两组不同的书签,根据要执行的操作,宏首先删除该特定操作不需要的书签,然后将数据粘贴到其余书签所在的位置。

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. 我只在这里放置了数组的3个元素,而实际上有3个元素,但是其思想是使“ AND”功能自动化,范围如下:如果Word文件中已经存在的每个书签都不同于ANY元素删除数组“ arr”,则必须删除该书签,否则不会发生任何反应。 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. 因此,我不想在数组的每个元素上使用一个AND函数,而是想遍历数组,以便将每个书签与数组的所有元素进行比较,并且前提是要删除所有不同的书签。 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". 我不知道是否有某种方式可以循环“ AND”功能,但是向数组中添加更多元素将意味着添加相同数量的“ 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 如果只有几个元素,则可以使用“ If”和遍历数组的方式来使用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 您可以使用Join()函数从所有书签数组值中获取一个字符串,然后使用Instr()函数检查当前的书签是否存在

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  

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

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