Как исправить ошибку по замене картинок в word?

Ошибки показаны в Коментарии

    Sub replaceImage(Number As Byte, imgName As String)
    
        Dim originalImage As InlineShape
        Dim newImage As InlineShape
    
        Set originalImage = ActiveDocument.InlineShapes(Number)
    
        Dim imageControl As ContentControl
    
        If originalImage.Range.ParentContentControl Is Nothing Then
            Set imageControl = ActiveDocument.ContentControls.Add(wdContentControlPicture, originalImage.Range)
        Else
            Set imageControl = originalImage.Range.ParentContentControl
        End If
    
        Dim imageW As Long
        Dim imageH As Long
        imageW = originalImage.Width
        imageH = originalImage.Height
    
        originalImage.Delete
    
        Dim imagePath As String
        Dim myIlsh As InlineShape
        imagePath = ActiveDocument.Path & Application.PathSeparator & "img\" & imgName
        Set myIlsh = ActiveDocument.InlineShapes.AddPicture(imagePath, False, True, imageControl.Range)
        
        
        With imageControl.Range.InlineShapes(Number) ' Тут ошибка при втором и последующем запуске процедуры (типо выхода за пределы массива)
                    .Height = imageH
                    .Width = imageW
               End With
        
    
         
    End Sub

Sub Main()
replaceImage 1, "pic1.png"
replaceImage 3, "pic2.png" ' Начиная от сюда появляются ошибки
replaceImage 4, "pic3.png"

End Sub

введите сюда описание изображения


Ответы (0 шт):