View Single Post
 
Old 12-10-2015, 02:57 AM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,523
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by addey View Post
the png images I want to include in the document are also sequentially numbered, and I know there is a way to walk through all InlineShapes in the document (i.e. sequential). And since it is also not a problem inserting an image at a selection, why would this be impossible?
Sure, inserting images into a document in a sequential order is quite straightforward. I've posted code here that can do that for hundreds of images. That's not the problem; the problem is in relating those images to the ones already in the document that you want to replace. The location sequence in the document may or may not be the same as your replacement image sequence. Unless you can be sure they are in the same order, you'd be better off doing the lot manually. Assuming the inlineshapes are in the same order as their replacements, you could use code like:
Code:
Sub ReplaceInlineImages()
Application.ScreenUpdating = False
Dim strFolder As String, i As Long, Rng As Range
Dim sngWdth As Single, sngHght As Single, iShp As InlineShape
strFolder = GetFolder
If strFolder = "" Then Exit Sub
With ActiveDocument
  For i = .InlineShapes.Count To 1 Step -1
    Set Rng = .InlineShapes(i).Range
    With Rng
      With .InlineShapes(1)
        sngWdth = .Width
        sngHght = .Height
        .Delete
      End With
      Set iShp = .InlineShapes.AddPicture(FileName:=strFolder & _
        "\image" & i & ".png", LinkToFile:=False, SaveWithDocument:=True)
      With iShp
        .Width = sngWdth
        .Height = sngHght
      End With
    End With
  Next
End With
Set Rng = Nothing: Set iShp = Nothing
Application.ScreenUpdating = True
End Sub
 
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
The above code lets you choose the source folder and, as well as replacing the original images, rezises the new ones to the original sizes.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote