title: Shapes.AddPicture in Word Table
body: <p>I am using a Word table as a placeholder for images, where table cells contain only pictures and no text.</p>
  
  <p>When inserting a picture into a Word table, I have no problems when inserting an Inline Shape.  The picture appears into the expected cell. However, with the "equivalent" code which inserts the picture as a Shape, the shape does not always appear in the expected cell.  So far, I have seen this problem in Word 2013, 32 bit version.</p>
  
  <pre><code>Sub test()
      Dim s As Shape
      Dim x As String
      Dim f As String
      Dim r As Long
      Dim c As Long
      Dim h As Single
      Dim w As Single
      Dim rng As Word.Range
      Dim ins As Word.InlineShape
  
      f = "file name of a picture, .bmp .jpg etc."
  
      Word.Application.ScreenUpdating = False
  
      If Selection.Information(wdWithInTable) Then
          ' insert a picture in a table cell
          r = Selection.Information(wdStartOfRangeRowNumber)
          c = Selection.Information(wdStartOfRangeColumnNumber)
  
          With Selection.Tables(1).Cell(r, c)
              Set rng = .Range
              rng.collapse wdCollapseStart
              .Range.Text = ""
              h = .height
              w = .width
          End With
  
          ' Works reliably
          Set s = Word.Selection.InlineShapes.AddPicture(f, False, True, rng).ConvertToShape
          s.height = h
          s.width = w
  
          ' Not at all reliable
          ' Set s = Word.ActiveDocument.Shapes.AddPicture(f, False, True, 0, 0, w, h, rng)
  
      Else
          ' insert a picture at the cursor
          h = 100
          w = 100
          Set s = Word.ActiveDocument.Shapes.AddPicture(f, False, True, 0, 0, w, h)
      End If
  
      Word.Application.ScreenUpdating = True
  
      s.WrapFormat.Type = wdWrapInline
      s.Title = "Title"
      s.AlternativeText = "Some metadata"
  End Sub
  </code></pre>
  
  <p>The idea is to select either a cell in a table in a document or somewhere on the page outside of the table.  The outside of the table case works as expected where the picture appears at the cursor location.</p>
  
  <p>To see the problem, start with a fresh document, single page, add a 3 x 3 table and deepen the rows a bit.  Be sure to supply a file to insert, variable <code>f</code>. Select one of the cells, then run the code.  This works correctly when the picture is inserted as an inline shape then immediately converted to a shape.  That happens with this line:</p>
  
  <pre><code>Set s = Word.Selection.InlineShapes.AddPicture(f, False, True, rng).ConvertToShape
  </code></pre>
  
  <p>However, the preferred solution would be to insert a Shape from the beginning with code something like this:</p>
  
  <pre><code>Set s = Word.ActiveDocument.Shapes.AddPicture(f, False, True, 0, 0, w, h, rng)
  </code></pre>
  
  <p>The picture appears, but usually not in the expected location.  It could be placed into a different cell or somewhere outside the table.</p>
  
  <p>Is the <code>rng</code> argument to Shapes.AddPicture being ignored or mangled somehow?</p>
  
  <p>Experimenting some more with the 3 x 3 table - adding pictures then setting every possible <code>WrapFormat.Type</code> (there are 8 possible values), I see that:</p>
  
  <ul>
  <li>for every <code>WrapFormat.Type</code> except <code>wdWrapInLine</code>, picture insertion works correctly as long as they are done from left to right on a table row, and;</li>
  <li>for every <code>WrapFormat.Type</code> without exception, when the row is initially empty, pictures inserted in columns 2 or 3 appear one column to the left.</li>
  </ul>
  
  <p>Making the picture smaller, such as setting <code>h = .height * 0.5</code> and <code>w = .width * 0.5</code>, has no effect on placement.</p>
  
  <p>Thanks very much for any insight or elucidation.</p>