VBA Word: Deleting InlineShapes seems to change the end of For Loop

做~自己de王妃 提交于 2020-05-17 07:46:30

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!