Consulting

Results 1 to 3 of 3

Thread: Finding and replacing pictures

  1. #1
    VBAX Regular
    Joined
    Mar 2017
    Posts
    10
    Location

    Finding and replacing pictures

    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

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Mar 2017
    Posts
    10
    Location
    Thank you so much Greg, this is absolutely spot on.

    Much appreciated,
    Andy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •