0
votes

I have a word document containing embedded filenames of photos (JPEGS) listed within it. My objective is to update the word document automatically with the actual photos listed within the document. I expect to write a macro to search the word document for each occurence of a filename and insert the actual JPEG object into the Word document immediately following the filename. I need to continue this process until I have found all JPEG filenames in the word document. Each filename is 26 characters long and they all begin with "IMG". As a newbie I am stuck on how to copy the text of the filename into a VBA variable.

The following is a sample of the word document:

Sunday 16 February 2014

9:52 PM Tom, the before and after photos are attached.

Tags: Construction Photos: IMG_0000320_2014.03.11.jpg, IMG_0000321_2014.03.11.jpg, IMG_0000322_2014.03.11.jpg

Thursday 13 February 2014

4:24 PM Tom, the drawings are attached. Please review.

Tags: Pre-Award Photos: IMG_0000323_2014.03.11.jpg, IMG_0000324_2014.03.11.jpg, IMG_0000325_2014.03.11.jpg, IMG_0000326_2014.03.11.jpg

My initial coding attempt is provided below: (I have not yet attempted the DO WHILE logic)

Dim JPEGName As String
Selection.Find.ClearFormatting
With Selection.Find
    .Text = "IMG"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=26, Extend:=wdExtend
JPEGName = Selection.Copy
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeParagraph

Selection.InlineShapes.AddPicture FileName:= _
    JPEGName _
    , LinkToFile:=False, SaveWithDocument:=True
Selection.TypeParagraph
End Sub

I am receiving a Compile Error: Expected Function or variable on the Selection.Copy statement.

1

1 Answers

0
votes

Something like this maybe:

Sub InsertPics()
Const PATH_TO_PICS As String = "C:\_Stuff\Test\"
Dim JPEGName As String

    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "IMG*.jpg"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With

    Do While Selection.Find.Execute()
        JPEGName = Selection.Text
        Selection.Collapse wdCollapseEnd
        Selection.TypeParagraph
        Selection.InlineShapes.AddPicture _
             FileName:=PATH_TO_PICS & JPEGName, _
             LinkToFile:=False, _
             SaveWithDocument:=True
        Selection.TypeParagraph

    Loop

End Sub