none
VBA INSERT PICTURE IN 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

    Wednesday, November 21, 2012 9:33 AM

Answers

  • Try

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

    Note that Left and Top were promoted from optional to required between XL2003 and XL2010 - width and height are still optional.


    Wednesday, November 21, 2012 8:16 PM

All replies

  • Though the arguments for AddPicture are shown as optional, just add the extra values (left,top, width, height) like this and it should work.

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


    Wednesday, November 21, 2012 2:41 PM
  • Thanks Bernie

    I did as you suggested and now I have a Complie error which says "expected named parameter"

    Wednesday, November 21, 2012 7:53 PM
  • Try

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

    Note that Left and Top were promoted from optional to required between XL2003 and XL2010 - width and height are still optional.


    Wednesday, November 21, 2012 8:16 PM
  • Good man Bernie, Thanks a million. 
    Thursday, November 22, 2012 9:00 PM