none
Add a picture to a Drawing Canvas RRS feed

  • Question

  • I am trying to add a picture to a drawing canvas in word but the code below gets a runtime error which says

    "Argument not Optional"

    I am using word 2007

    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 _
            FileName:="C:\Program Files\Microsoft Office\" & _
                "Office\Bitmaps\Styles\stone.bmp", _
            LinkToFile:=False, SaveWithDocument:=True
    End Sub

    • Moved by Reed KimbleMVP Tuesday, November 20, 2012 9:04 PM posted in vb.net general (From:Visual Basic General)
    Tuesday, November 20, 2012 8:34 PM

Answers

  • Hi "irish"

    Sorry it took me so long - I had to dig that old machine out, and it wasn't particularly happy about having to go back to work...

    The first time I ran the code in Word 2007 I got a different error message, first. After going into Debug and pressing F8 to force it to run again I then got the error message you report. So there is definitely an "oddity" in Word 2007.

    I couldn't get it to display that first error message again, even after closing and restarting Word. The impression it made on me was that something to do with the Canvas type of Shape wasn't hooked up properly. A second try got VBA to recognize a Shape, but it still wasn't able to deal with the optional Parameters associated with CanvasItems.AddPicture as optional parameters.

    The method works fine, however, if you also specify the Left, Top, Height and Width parameters.


    Cindy Meister, VSTO/Word MVP, my blog

    Sunday, November 25, 2012 3:07 PM
    Moderator

All replies

  • This would be a question for the VBA forums.  These forums are for .Net applications.

    I'll see if I can find a more appropriate forum.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, November 20, 2012 9:03 PM
  • Hi "irish"

    The AddPicture method takes the following parameters: (FileName As String, [LinkToFile], [SaveWithDocument], [Left], [Top], [Width], [Height])

    Depending on the programming language (which you don't specify, but I'm guessing VB.NET) and the setttings (if VB.NET, whether you have Option Strict On or Off) the compiler may be expecting you to pass values for all six...


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, November 21, 2012 1:54 PM
    Moderator
  • Thanks "Cindy"

    I am using  VBA


    • Edited by paddy irish Wednesday, November 21, 2012 2:33 PM
    Wednesday, November 21, 2012 2:27 PM
  • Which line is highlighted when the error appears?

    I just tried the code in Word 2010 and it worked fine (once I had a valid file path). I have to fire up another machine in order to test 2007, so I'd like as much information as possible before I do anything more...


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, November 21, 2012 3:22 PM
    Moderator
  •  The following lines are all highlighted.

    shpCanvas.CanvasItems.AddPicture _
            FileName:="C:\Program Files\Microsoft Office\" & _
                "Office\Bitmaps\Styles\stone.bmp", _
            LinkToFile:=False, SaveWithDocument:=True

    Wednesday, November 21, 2012 7:59 PM
  • Hi "irish"

    Sorry it took me so long - I had to dig that old machine out, and it wasn't particularly happy about having to go back to work...

    The first time I ran the code in Word 2007 I got a different error message, first. After going into Debug and pressing F8 to force it to run again I then got the error message you report. So there is definitely an "oddity" in Word 2007.

    I couldn't get it to display that first error message again, even after closing and restarting Word. The impression it made on me was that something to do with the Canvas type of Shape wasn't hooked up properly. A second try got VBA to recognize a Shape, but it still wasn't able to deal with the optional Parameters associated with CanvasItems.AddPicture as optional parameters.

    The method works fine, however, if you also specify the Left, Top, Height and Width parameters.


    Cindy Meister, VSTO/Word MVP, my blog

    Sunday, November 25, 2012 3:07 PM
    Moderator