问题
I have encountered a strange behaviour in VBA. I have written a code that deletes InlineShapes of certain size (and write them into a txt File). The code results in an error because the end of my for loop seems to change during running. Can someone help?
Function findAndRemovePics()
Dim i As Integer
Dim count As Integer
Dim str As String
currpath = "..."
'Set Objects
Set xlApp = CreateObject("Excel.Application")
Set xlWB = xlApp.Workbooks.Open(currpath & "linkliste_http.xlsx") 'Replace String with path to Excel File
Set xlWS = xlWB.Worksheets("Sheet1") 'Replace String with your Worksheet Name
myFile = currpath & "imagelist.txt"
Open myFile For Output As #1
With ActiveDocument
inlinenr = .InlineShapes.count
'loop through inline shapes
For i = 1 To inlinenr
sngWdth = .InlineShapes(i).Width
SngHght = .InlineShapes(i).Height
If (sngWdth = SngHght And SngHght < 80 And sngWdth < 80 And sngWdth > 20 And SngHght > 20) Then
ActiveDocument.InlineShapes(i).Select
ActiveDocument.InlineShapes(i).Delete
str = count & " " & sngWdth & " " & SngHght & vbNewLine
Print #1, str
count = count + 1
End If
Next i
End With
Close #1
End Function
来源:https://stackoverflow.com/questions/61756753/vba-word-deleting-inlineshapes-seems-to-change-the-end-of-for-loop