title: Word VBA - Inserting Inline Picture from filepath in document
body: <p>I have a Word Document that includes as text the complete filepaths to multiple images (e.g. C:\Users\Name\Documents\Test Logos\alphatest.png). I am trying to create a macro to replace each text filepath with the image it refers to as inline shapes. The script also resizes the images. I am having trouble assigning a valid reference to the inline shape object variable using the Set statement. </p>
  
  <p>((Right now, I am locating the filepaths in the Word document by manually putting "QQQ" before and after the text in the Word Document and then having the script search for text that is flanked by "QQQ." So, in the Word Document, each filepath looks like this: "QQQC:\Users\Name\Documents\Test Logos\alphatest.pngQQQ". This is a temporary kludge and does not seem to be the source of the error.))</p>
  
  <pre><code>Sub InsertAndResizeLogos()
  '
  ' InsertAndResizeLogos Macro
  ' Insert logos at the correct place in the document, resize to less than 1 inch tall and 2 inches wide.
  '
      Selection.HomeKey Unit:=wdStory
      With Selection.Find
          .ClearFormatting
          .Text = "QQQ*QQQ"
          .Forward = True
          .Wrap = wdFindStop
          .Format = False
          .MatchCase = False
          .MatchWholeWord = False
          .MatchAllWordForms = False
          .MatchSoundsLike = False
          .MatchWildcards = True
          Do While .Execute
              While Selection.Find.Found
                  Dim imagePath As String
                  Debug.Print Replace(Selection.Text, "QQQ", "")
                  imagePath = Replace(Selection.Text, "QQQ", "")
                  imagePath = Replace(imagePath, "\", "//")
                  imagePath = Replace(imagePath, vbCr, "")
                  Debug.Print imagePath
  
                  Dim SHP As InlineShape
                  Set SHP = Selection.InlineShapes.AddPicture(FileName:=imagePath, LinkToFile:=False, SaveWithDocument:=True)
                      SHP.LockAspectRatio = True
                      SHP.Height = InchesToPoints(1)
                  If SHP.Width &gt; InchesToPoints(2) Then
                      SHP.Width = InchesToPoints(2)
                  End If
              Wend
          Loop
      End With
  
  End Sub
  </code></pre>
  
  <p>If I don't convert the filepath string to VBA's preferred format (i.e., removing this line from the script:)</p>
  
  <pre><code>                imagePath = Replace(imagePath, "\", "//")
  </code></pre>
  
  <p>then the script successfully combs through the Word Document, finds the first filepath, and replaces it with the correct image. But then it throws a "Runtime Error 5152: This is not a valid file name." on the "Set" line and breaks.</p>
  
  <p>If I <em>do</em> convert the filepath string to VBA format by replacing the \'s with //'s, then it does not successfully insert the image and throws a "Runtime Error 91: Object variable or With block variable not set" on the SHP.LockAspectRation=True line and breaks.</p>
  
  <p>It seems like if I feed the filepath into the Set statement with //'s, it can no longer find the image. Is this something I could fix with error handling, or am I making a more fundamental mistake?</p>
  
  <p>((If I set the filepath <em>within the script</em>, (i.e. imagePath = C:\Users\Name\Documents\Test Logos\alphatest.png), the script will successfully iterate through the entire document and replace all text with the QQQ's with that image.))</p>
  
  <p><strong>SOLUTION</strong></p>
  
  <p>Here is the final code that worked correctly:</p>
  
  <pre><code>    Sub InsertAndResizeLogos()
  '
  ' InsertAndResizeLogos Macro
  ' Insert logos at the correct place in the document, resize to less than 1 inch tall and 2 inches wide.
  '
  Application.ScreenUpdating = False
  Dim i As Long, j As Long, StrNm As String, StrErr As String, iShp As InlineShape
  With Selection 'ActiveDocument.Range
    With .Find
      .ClearFormatting
      .Text = "*.[A-Za-z]{3}&gt;"
      .Forward = True
      .Wrap = wdFindContinue
      .Format = False
      .MatchWildcards = True
      .Execute
    End With
    Do While .Find.Found
      StrNm = .Text
      If Dir(StrNm) = "" Then
        j = j + 1: StrErr = StrErr &amp; vbCr &amp; StrNm
      Else
        i = i + 1
        Set iShp = .InlineShapes.AddPicture(FileName:=StrNm, LinkToFile:=False, SaveWithDocument:=True)
        With iShp
          .LockAspectRatio = True
          .Height = InchesToPoints(1)
          If .Width &gt; InchesToPoints(2) Then .Width = InchesToPoints(2)
        End With
      End If
      .Collapse wdCollapseEnd
      .Find.Execute
    Loop
  End With
  Application.ScreenUpdating = True
  MsgBox i &amp; " images added." &amp; vbCr &amp; j &amp; " image files not found:" &amp; StrErr
  End Sub
  </code></pre>
  
  <p>The problem seems to have been related to pulling the filepath from Selection.Text rather than from .Find.Found.Text</p>
  
  <p>This mostly uses the approach suggested below by Macropod, although applied to Selection rather than to Document.Range to maintain the "replace the text with the image" functionality. For some reason, Find.Execute's ReplaceWith parameter and Find's Replacement property refused to work no matter where in the process I called them.</p>