I have already checked what I am asking now but I couldn't still solve my problem which is the following:
I try to delete text between two bookmarks in a Word document - that part I have solved. However, I have many bookmark pairs and I would like to delete text in between each pair. So, I decided to enter the bookmarks in two arrays: arr_B for beginning of the text part and arr_E for the end of the text part and between them text should be deleted. I have tried like this:
Sub Macro2()
Dim arr_B As Variant
Dim arr_E As Variant
Dim i As Long
Dim k As Long
Dim element As Variant
arr_B = Array("B1_RSD1", "B2_RSD2")
arr_E = Array("E1_RSD1", "E2_RSD2")
For Each element In arr_B
For Each element In arr_E
Set rngStart = ActiveDocument.Bookmarks(i).Range
Set rngEnd = ActiveDocument.Bookmarks("k").Range
ActiveDocument.Range(rngStart.Start, rngEnd.End).Select
Selection.Delete
Exit For
Next k
Next i
End Sub
However, above code gives the error
For control variable already in use
I couldn't find out how to solve this problem. I would appreciate any help and many thanks in advance.
Given the similarity of your bookmark names, it appears you don't even need an array. Try:
Sub Demo()
Application.ScreenUpdating = False
Dim i As Long, Rng As Range, StrTxt As String
With ActiveDocument
For i = 1 To .Bookmarks.Count / 2
StrTxt = i & "_RSD" & i
If .Bookmarks.Exists("B" & StrTxt) And .Bookmarks.Exists("E" & StrTxt) Then
Set Rng = .Bookmarks("B" & StrTxt).Range
With Rng
.End = .Bookmarks("E" & StrTxt).Range.End
.Delete
End With
End If
Next i
End With
Application.ScreenUpdating = False
End Sub
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.