Log in

View Full Version : Finding and replacing pictures



andysjp
03-10-2017, 09:15 AM
Hi all

I am trying to write a macro to replace some old graphics in a Word document with new versions.

The only way I guess it's possible is for the macro to look at each picture in turn, figure out it's height and width (which will then identify which picture it is, as each picture is sized differently, luckily) and then replace it with its updated version (which is saved on my desktop as a JPG).

So for example, in my document, if there are several pictures of a raincloud which are all 1.5cm tall x 1.7cm wide, and several pictures of a sun which is 1.8cm wide x 1.8cm tall, the macro would find the first picture, see that it is 1.8cm x 1.8cm, determine that it must be a sun picture, it then needs to delete it and replace it (in the same place) with the newer version of the sun from my desktop.

I have hundreds of documents with these pictures, hence why I need a macro.

I believe the pictures are all inline shapes.

Happy for the macro to work in pixels/points rather than cm.

Please can anyone help?

Thanks in advance
Andy

gmaxey
03-10-2017, 04:31 PM
Perhaps something like this:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oILS As InlineShape, oILSNew As InlineShape
Dim lngIndex As Long
Dim oRNg As Range
For lngIndex = ActiveDocument.Range.InlineShapes.Count To 1 Step -1
Set oILS = ActiveDocument.Range.InlineShapes(lngIndex)
If oILS.Height = 72 And oILS.Width = 96 Then
Set oRNg = oILS.Range
Set oILSNew = ActiveDocument.InlineShapes.AddPicture("D:\Test.png", False, True, oRNg)
With oILSNew
oILSNew.LockAspectRatio = oILS.LockAspectRatio
oILSNew.Width = oILS.Width
End With
oILS.Delete
End If
Next
lbl_Exit:
Exit Sub

End Sub

andysjp
03-13-2017, 02:21 AM
Thank you so much Greg, this is absolutely spot on.

Much appreciated,
Andy