Results 1 to 11 of 11
  1. #1
    New Lounger
    Join Date
    Aug 2016
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Word Macro to insert a file

    I have a Word file that looks something like this:

    Here is some text.
    IMAGE:c:\project\pictures\mypict1.jpg
    Here is some more text.
    IMAGE:c:\project\pictures\mypict2.jpg
    And some more.
    IMAGE:c:\project\pictures\mypict3.jpg
    And the final text.

    I need a macro which will search for the label "IMAGE:", cut the file name, and insert that picture at that location in the document. (Ideally, it would repeat that process until the search failed - i.e., to insert all of the images - but I could run the macro multiple times if necessary.)

    I created a macro that does most of the work but when I choose Insert>Picture, it won't let me paste the file name into the file selector dialog. Or more precisely, it lets me paste the file name value but the macro file records the literal value that I pasted while recording, not the instruction to paste.

    Is there a way to do this?


    I'm using Word 2013 if that matters.

    Thanks for any help.

  2. #2
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts
    Try: Selection.Paste

  3. #3
    New Lounger
    Join Date
    Aug 2016
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    It was:

    Selection.InlineShapes.AddPicture FileName:= _
    "c:\project\pictures\mypict1.jpg", LinkToFile:= _
    False, SaveWithDocument:=True

    but this

    Selection.InlineShapes.AddPicture FileName:= _
    Selection.Paste, LinkToFile:= _
    False, SaveWithDocument:=True

    produces a compile error.

    Am I understanding you correctly?

    Thanks for your response.

  4. #4
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts
    So it's not the image you're cutting and pasting -- it's the filename. Assign the file name to a variable (you haven't supplied that part of the code), and then use the variable as the picture name. In the above code, where did you get "c:\project\pictures\mypict1.jpg"?

  5. #5
    Star Lounger Graham Mayor's Avatar
    Join Date
    Mar 2016
    Location
    Cyprus
    Posts
    68
    Thanks
    0
    Thanked 24 Times in 24 Posts
    Assuming that the texts as shown comprise separate paragraphs i.e.

    Here is some text.¶
    IMAGE:c:\project\pictures\mypict1.jpg¶
    Here is some more text.¶
    IMAGE:c:\project\pictures\mypict2.jpg¶
    And some more.¶
    IMAGE:c:\project\pictures\mypict3.jpg¶
    And the final text.¶

    the following macro will insert the images named, if accessible

    Code:
    Option Explicit
    Sub InsertImages()
    Dim oRng As Range
    Dim strImage As String
    Dim strMissing As String: strMissing = ""
    Dim fso As Object
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set oRng = ActiveDocument.Range
        With oRng.Find
            Do While .Execute(FindText:="IMAGE:", MatchCase:=True)
                oRng.MoveEndUntil Chr(13)
                strImage = Split(oRng.Text, Chr(58))(1)
                strImage = strImage & Chr(58) & Split(oRng.Text, Chr(58))(2)
                oRng.Text = "" 'Deletes the original text line
                If fso.FileExists(strImage) Then
                    oRng.InlineShapes.AddPicture _
                            FileName:=strImage, _
                            LinkToFile:=False, _
                            SaveWithDocument:=True
                Else
                    strMissing = strMissing & strImage & vbCr
                End If
                oRng.Collapse 0
            Loop
        End With
        If Len(strMissing) > 0 Then MsgBox strMissing, , "The following images were not found"
    lbl_Exit:
        Set oRng = Nothing
        Set fso = Nothing
        Exit Sub
    End Sub
    Graham Mayor - Word MVP
    http://www.gmayor.com

  6. The Following User Says Thank You to Graham Mayor For This Useful Post:

    bobv (2016-08-09)

  7. #6
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts
    If you want to assign the current selection to a variable, use Selection.Value.

  8. #7
    New Lounger
    Join Date
    Aug 2016
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks very much, Graham! That's just what I needed. I really appreciate all of your effort.

    Sorry to impose but how could I also set the Layout>Wrapping property of the image to Tight?

    Thanks again.

  9. #8
    Star Lounger Graham Mayor's Avatar
    Join Date
    Mar 2016
    Location
    Cyprus
    Posts
    68
    Thanks
    0
    Thanked 24 Times in 24 Posts
    Convert the inline shape to a shape and set the wrap property of the shape and hope it doesn't screw up the layout of the document.

    Code:
    Option Explicit
    
    Sub InsertImages()
    Dim oRng As Range
    Dim strImage As String
    Dim strMissing As String: strMissing = ""
    Dim fso As Object
    Dim oILShape As InlineShape
    Dim oShape As Shape
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set oRng = ActiveDocument.Range
        With oRng.Find
            Do While .Execute(FindText:="IMAGE:", MatchCase:=True)
                oRng.MoveEndUntil Chr(13)
                strImage = Split(oRng.Text, Chr(58))(1)
                strImage = strImage & Chr(58) & Split(oRng.Text, Chr(58))(2)
                oRng.Text = ""    'Deletes the original text line
                If fso.FileExists(strImage) Then
                    Set oILShape = oRng.InlineShapes.AddPicture _
                                   (FileName:=strImage, _
                                    LinkToFile:=False, _
                                    SaveWithDocument:=True)
                    Set oShape = oILShape.ConvertToShape
                    oShape.WrapFormat.Type = wdWrapTight
                Else
                    strMissing = strMissing & strImage & vbCr
                End If
                oRng.Collapse 0
            Loop
        End With
        If Len(strMissing) > 0 Then MsgBox strMissing, , "The following images were not found"
    lbl_Exit:
        Set oRng = Nothing
        Set fso = Nothing
        Set oShape = Nothing
        Set oILShape = Nothing
        Exit Sub
    End Sub
    Graham Mayor - Word MVP
    http://www.gmayor.com

  10. #9
    New Lounger
    Join Date
    Aug 2016
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I'm afraid that this code produces an error:

    Run-time error -2147467259 (80004005)
    Method 'ConvertToShape' of InlineShape failed

    Thanks again for your help, Graham.

  11. #10
    Star Lounger Graham Mayor's Avatar
    Join Date
    Mar 2016
    Location
    Cyprus
    Posts
    68
    Thanks
    0
    Thanked 24 Times in 24 Posts
    Which Word version are you using? I have tested this in Word 2007 and Word 2016 and it works fine with the example you posted. - See attached
    Attached Files Attached Files
    Graham Mayor - Word MVP
    http://www.gmayor.com

  12. The Following User Says Thank You to Graham Mayor For This Useful Post:

    bobv (2016-08-10)

  13. #11
    New Lounger
    Join Date
    Aug 2016
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Problem solved. I tried the macro with your TestDoc.docx and it worked - which told me that there was something wrong with my document. I'm generating it programmatically to RTF and realized that, while Word (2013) opens it, it doesn't convert it. So your macro was operating on an RTF file. I resaved it as a docx and the macro worked.

    Thanks again for your help.

Posting Permissions

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