none
Replace a series of InlineShapes with new images RRS feed

  • Question

  • Hi,

    I'm trying to do something very simple, but I'm not a VBA developer and am having a heck of a time finding the code that will do what I want. Any help that a VBA pro can provide would be greatly appreciated.

    I have a Word 2007 document that has several images throughout it. These images are stored as InlineShapes. I'm trying to write a simple macro that iterates through all the images and replaces each with a new image (in the form of an InlineShape) pulled from a file. With the experimenting I've done so far, I'm able to iterate through all the InlineShapes, select each one, and delete it (though I'm not sure that's necessary). I'm also able to insert a new image as an InlineShape. The problem is that when I do that, all the images appear one after the other at the top of the document. I'm assuming I need to somehow set the range to the location of the current InlineShape, but I can't quite figure out how to make it all work.

    I'd share the code I've got, but I have about ten different versions, none of which work the way I'd expect, so I'm not sure any of it would help. Can anyone help? I'm guessing that, done right, this is probably about five to ten lines of code.

    Thanks,

       Andy

     

    Saturday, January 22, 2011 1:37 AM

Answers

  • Hi Andy

    From the sound of it, I'd probably loop using For...Next and start with the last object, working backwards. something like
       For i = doc.InlineShapes.Count to 1, Step -1

    And yes, the problem is likely that you should be getting a Range. The InlineShape object does return a Range: doc.InlineShapes(index).Range

    So your method should look something like:
      Dim rng as Word.Range
      Set rng = doc.InlineShapes(index).Range
      ActiveDocument.InlineShapes.AddPicture("filename", fals, true, rng)

    That will probably replace the InlineShape in question, but if not, just delete it after you set the range object.


    Cindy Meister, VSTO/Word MVP
    Saturday, January 22, 2011 6:41 AM
    Moderator
  • That did it! Thank you very much for saving me hours of pulling out my hair!

    (BTW, I did need to delete the InlineShape.)

      - Andy

    Monday, January 24, 2011 9:39 PM

All replies

  • Hi Andy

    From the sound of it, I'd probably loop using For...Next and start with the last object, working backwards. something like
       For i = doc.InlineShapes.Count to 1, Step -1

    And yes, the problem is likely that you should be getting a Range. The InlineShape object does return a Range: doc.InlineShapes(index).Range

    So your method should look something like:
      Dim rng as Word.Range
      Set rng = doc.InlineShapes(index).Range
      ActiveDocument.InlineShapes.AddPicture("filename", fals, true, rng)

    That will probably replace the InlineShape in question, but if not, just delete it after you set the range object.


    Cindy Meister, VSTO/Word MVP
    Saturday, January 22, 2011 6:41 AM
    Moderator
  • That did it! Thank you very much for saving me hours of pulling out my hair!

    (BTW, I did need to delete the InlineShape.)

      - Andy

    Monday, January 24, 2011 9:39 PM
  • Hi there Andy,

    I know it's a long shot, since this is 7 years old, but I am trying to do exactly what you described and have not had success with this method. Could you please further explain the method or provide the code?

    Thanks,

    Saturday, June 9, 2018 10:28 AM