Share via

MS Word VB Script issue

Anonymous
2023-06-09T18:42:19+00:00

Hello I m writing a VB Script macro very simple code:

ActiveDocument.range.InlineShapes.AddPicture FileName:=strImageFile, LinkToFile:=False, SaveWithDocument:=True

When I run this macro it inserts me an empty space instead of an image where the cursor is, but if I select the area and then call macro it does insert the image.

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

5 answers

Sort by: Most helpful
  1. Jay Freedman 207.7K Reputation points Volunteer Moderator
    2023-06-09T23:30:26+00:00

    As Anna suggests, you can use the Selection.InlineShapes collection to place the picture where the insertion point is at the time the macro runs, so you'd have to click there before starting the macro.

    However, the ActiveDocument.InlineShapes.AddPicture method has an optional parameter named Range, with which the macro can specify any range in the active document, regardless of where the insertion point is. The syntax of the method is

    ActiveDocument.InlineShapes.AddPicture FileName, [LinkToFile], [SaveWithDocument], [Range]
    

    where each of the parameters shown in square brackets is optional.

    If you do want to place the picture at the current insertion point, you can write Range:=Selection.Range. If the document has a bookmark at the desired spot, you can write

    Range:=ActiveDocument.Bookmarks("bookmarkname").Range
    

    There are lots of other ways to determine where the correct range is (for example, the range of a cell in a table).

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2023-06-09T20:21:25+00:00

    Hi Alex

    I'm AnnaThomas and I'd happily help you with your question. In this Forum, we are Microsoft consumers just like yourself.

    It seems that the issue you’re experiencing is related to the Range object in your code. When you call the AddPicture method on the InlineShapes collection of the Range object without first selecting a specific range, it may not insert the image where you expect it to.

    One possible solution is to specify the range where you want to insert the image before calling the AddPicture method. For example, you could use the Selection object to get the current selection and then call the AddPicture method on its InlineShapes collection:

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

    This should insert the image at the current cursor position.

    I hope this helps ;-), let me know if this is contrary to what you need, I would still be helpful to answer more of your questions.

    Best Regards,

    AnnaThomas

    Give back to the community. Help the next person with this problem by indicating whether this answer solved your problem. Click Yes or No at the bottom.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  3. Anonymous
    2023-06-10T10:11:41+00:00

    Alright Alex, you keep us posted after trying what Jay suggested.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2023-06-10T04:51:40+00:00

    Thank you Jay, will try it!

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2023-06-10T00:34:07+00:00

    Thank you AnnaThomas. That’s exactly how I call it but at times it simply inserts a space. When I make few empty lines as per the image height and select them then it inserts.

    I notice it fails to insert an image at customer position sometime. That’s very weird.

    Was this answer helpful?

    0 comments No comments