Share via

Word VBA: ConvertToShape method makes image disappear

Anonymous
2016-10-13T10:52:15+00:00

I wrote some code for a client which isn't working correctly on his machine (Win 10, Office 365) but is on mine (Win 10, Office 2016). The code inserts an image to the header then positions it and resizes it. I use the ConvertToShape method so I can access the .Left property of the Shape class.

Dim pic As Shape
Dim shp As InlineShape

Set shp = thisDocument.Sections.Item(1).Headers(wdHeaderFooterPrimary).Range.InlineShapes.AddPicture(fpImage) ' insert the image to the header

Set pic = shp.ConvertToShape ' THIS LINE CAUSES THE PROBLEM

The method causes the image to disappear. 'Pic' is still available and setting it's properties causes no error, but it is not visible. It's .visible property returns true.

Specifically what I need to do is insert the image to the right hand side of the header without affecting any text already in the header, which should stay left aligned so aligning the whole paragraph won't work.

Any ideas on how to achieve this without ConvertToShape? Thanks.

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Jay Freedman 207.7K Reputation points Volunteer Moderator
2016-10-14T03:05:13+00:00

I see that you posted this question last week on StackOverflow and didn't get any useful answer. (It helps to mention that you're cross-posting, so people don't spend time if you've already gotten an answer.)

There is a way to do this with only an inline shape, by setting up a table to position the text on the left and the picture on the right. An additional advantage of this method is that, if you set the table's AutoFitBehavior property to wdAutoFitFixed and set the column width to the width you want for the shape, Word will automatically resize the picture to that width and keep the aspect ratio.

Here's a little sample macro:

Sub x()

   Dim fpImage As String

   Dim strExistingHeaderText

   Dim tbl As Table

   Dim shp As InlineShape

   fpImage = "D:\Pictures\bunnycakes.jpg"

   With ActiveDocument

      strExistingHeaderText = _

         .Sections(1).Headers(wdHeaderFooterPrimary).Range.Text

      Set tbl = .Tables.Add( _

         Range:=.Sections(1).Headers(wdHeaderFooterPrimary).Range, _

         numrows:=1, numcolumns:=2, _

         AutoFitBehavior:=wdAutoFitFixed)

      tbl.Columns(2).Width = InchesToPoints(1.5)

      tbl.Columns(1).Width = InchesToPoints(5#)

      tbl.Cell(1, 1).Range.Text = strExistingHeaderText

      'tbl.Borders.Enable = False

      Set shp = tbl.Cell(1, 2).Range.InlineShapes.AddPicture(fpImage)

   End With

End Sub

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Jay Freedman 207.7K Reputation points Volunteer Moderator
    2016-10-15T00:47:21+00:00

    What could go wrong with ConvertToShape is that it could position the new shape completely outside the page. Your snippet didn't include any of the settings you tried to change afterward. Although you mentioned using the .Left property, that wouldn't necessarily bring the picture into view if, say, the .Top property was set to a negative or very large positive value.

    I'll head over to StackOverflow and add the answer there -- good point.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2016-10-14T09:19:08+00:00

    Thanks, that was a good idea about using a table.

    I'm curious as to what could have been going wrong with the ConvertToShape method.

    I'll bear in mind about cross-posting. Likewise it would be great if you could copy your answer to StackOverflow as well as posting a link; S.O. have a guideline against link only posts in case the link goes dead.

    Thanks again,

    Darren

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2016-10-14T08:23:19+00:00

    Thanks Ian, I did a thorough search before posting this question

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2016-10-14T02:31:47+00:00

    Hello,

    Please refer our MSDN forums for further assistance regarding your concern.

    Let us know if you have further concerns.

    Was this answer helpful?

    0 comments No comments