I would like to create a Word macro to separate text and images. The macro should insert the text at first place and move the images inside the text to the end of the document and replace the image with an index showing the number of the image, starting from the first image to the last image. I would appreciate if someone could help with this.

I have looked in many sites and I have developed the following code. This code simply replaces any image in the document with a text which is a kind of index. For example, it replaces the first image with just simple index as “image1.jpg” and so on. But my problem is that I don’t want to just replace the image with a numbered text index. My goal is also to move for example the image 1 to the end of the document. This should happen for the second image in the document which is replacing the 2nd image with a numbered index and then moving the image to the end of the document (after the first image) and so on for other images. I guess that this code must be added with some other code to make this happen. if someone could help, it will help a lot for making web pages suitable for printing on papers using Microsoft word macro. because sometimes it is not always necessary to print the images in the document and the text alone would be enough.

Dim oILShp As InlineShape
Dim ILShpIndex As Integer
For Each oILShp In ActiveDocument.InlineShapes
    ILShpIndex = ILShpIndex + 1
    'insert text in place where InlineShape is located
    ActiveDocument.Range(oILShp.Range.Start, oILShp.Range.End).Text = _
                                "[Image" & ILShpIndex & ".Jpg]"
    'delete picture is not needed - it was simply replaced with text


The following will loop through the inline shapes, add paragraphs to the end of the document and move the inline shapes there

Dim oILShp As InlineShape
Dim ILShpIndex As Long
Dim oPara1 As Paragraph

With ActiveDocument
    For ILShpIndex = 1 To .InlineShapes.Count
        Set oILShp = .InlineShapes(1)
        Set oPara1 = .Content.Paragraphs.Add
        oPara1.Range.FormattedText = oILShp.Range.FormattedText
        .Range(oILShp.Range.Start, oILShp.Range.End).Text = "[Image" & ILShpIndex & ".Jpg]"
    Next ILShpIndex
End With

Note that since you need a counter anyways, you can use it to loop through the inlineShapes, using For Each oILShp In ActiveDocument.InlineShapes will cause you to loop infinitely through the shapes you would keep moving to the end of the document.

  • Thank you. it is working. with this, it will be easy to copy a website content including images and then pasting in word. Finally, I can choose whether to print the images or not since they are not inside the text. – Veronica Jan 2 at 12:12
  • If this answer works for you, you should mark it as correct -- we don't do this for free after all :) – cybernetic.nomad Jan 2 at 13:25

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.