Share via

CanvasItems.AddPicture runtime.error '5'

Anonymous
2017-07-11T16:19:23+00:00

I have been running a VBA macro in a word document (originally 2010, now 2016) that adds a canvas to a document, then inserts pictures in to the canvas.

This worked great. Until, last week when inexplicably the macro started failing at the AddPicture line, with:

Run-Time Error '5':

Invalid procedure call or argument.

I have changed nothing in the macro, but now it simply will not run!

To test this I created a brand new macro enabled word document & added a simple macro to replicate the behaviour: add a canvas, insert an image in the canvas. This also fails with the error above!

My test code is taken from the MSDN article "https://msdn.microsoft.com/en-gb/vba/word-vba/articles/canvasshapes-addpicture-method-word":

Sub NewCanvasPicture()

  Dim shpCanvas As Shape

  'Add a drawing canvas to the active document

  Set shpCanvas = ActiveDocument.Shapes _

  .AddCanvas(Left:=100, Top:=75, _

  Width:=200, Height:=300)

  'Add a graphic to the drawing canvas

  shpCanvas.CanvasItems.AddPicture "C:\Users\Public\Pictures\test.jpg", False, True

End Sub

I also tried explicitly naming the addPicture options. This also fails:

 shpCanvas.CanvasItems.AddPicture "C:\Users\Public\Pictures\test.jpg", LinkToFile:=False, SaveWithDocument:=True

I am beginning to suspect an update has broken word/office. Any assistance would be greatly accepted

OS Details:

Windows 10 Home, 64-Bit

Version 1703

OS Build 15063.447

Office Details:

Version 1706 (Build 8229.2073 Click-to-Run)

Current Channel

VBA Details:

Microsoft Visual Baic for Applications 7.1

Version 1068

VBA Retail 7.1.1068

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

Anonymous
2017-11-14T14:15:35+00:00

Hi JDN_321,

As of writing this, still have this error (on a 64-bit version of Word 2016)! Further, I'm also getting no response to this long outstanding issue.

I even tried to directly contact MS support, but they would only discuss the issue if I paid for a support call! Considering this was a longstanding macro that had worked flawlessly until some mysterious "change" in the environment rendered it useless, I chose to not pay.

While, to my knowledge, no solution exists for this error, I have devised a work-around. I'm really not happy about this as there is nothing to say the same problem will not occur with the alternative code in the future.

Having said that, here's a way to insert an image in a canvas that works for me, with a few caveats:

  1. Obviously the image file in the code has to exist in the specified location
  2. The shape has to be the created with correct dimensions for the inserted image

Sub NewPictureFill()

 Dim i As Integer

 Dim shpCanvas As Shape

 Dim shpPicture As Shape

 'Add a drawing canvas to the active document

 Set shpCanvas = ActiveDocument.Shapes _

 .AddCanvas(Left:=100, Top:=125, _

 Width:=400, Height:=400)

 'Add a graphic to the drawing canvas

 Set shpPicture = shpCanvas.CanvasItems.AddShape(Type:=msoShapeRectangle, Left:=25, Top:=25, Width:=90, Height:=90)

 shpPicture.Fill.UserPicture "C:\Users\Public\Pictures\test.jpg"    'CHANGE THIS to image file on your PC!

End Sub

While there may be a clever way to automatically resize the shape to suit the inserted image, this is only a proof of concept test sub, so I have not taken it any further!

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous
    2017-11-18T10:58:04+00:00

    You're welcome :)

    Just a shame we need to go through this because of some random, unannounced change in Word VBA.

    I use image insertion as a critical part of my software, so will know about it *really* quickly if this also stops working

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2017-11-18T10:53:26+00:00

    Hi Glasco,

    Thanks, this works for me. 

    Let's hope it will also work in the next update of Office :-)

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2017-11-14T10:18:30+00:00

    Hi Glasco61,

    I have the same problem.

    Recently I had to install a 32-bit version of Office. I thought that maybe that's the reason of the error, but I don't know....

    If you get a solution, I would appreciate it if you could tell it.

    Thanks!

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2017-07-19T15:01:03+00:00

    bump!

    I continue to test this daily, after checking for Windows updates.

    But I'm still getting the error. But, not getting any response here [sadPandaFace]

    Was this answer helpful?

    0 comments No comments