Consulting

Results 1 to 5 of 5

Thread: Adding a picture to a Shape

  1. #1

    Adding a picture to a Shape

    I'm using Word 2016.

    Here's what I'm trying to do: In a document I have a Shape ("myShape"). I now want to add a picture to that Shape and to do it manually, I select it then go to Format (Drawing Tools)>Shape Fill>Picture... and from the Insert File dialog box I select an appropriate picture and it inserts it OK. No problems!

    Then I thought I'd like to see whether the Macro recorder would record those steps. I tried it but all it would record was:
    ActiveDocument.Shapes.Range(Array("myShape")).Select
    Then I decided to research what others might have done and cobbled together a procedure:
    Sub InsertPictureDialog()
        Dim oDialog         As Word.Dialog
        Dim strName As String
         
    'Get a handle to the Insert picture dialog
        Set oDialog = Dialogs(wdDialogInsertPicture)
         
         ' Work with dialog
        With oDialog
             ' Display the dialog
            .Display
             
             'Insert Shape Picture if the Name property (Filepath) <> ""
            If .Name <> "" Then
                strName = ActiveDocument.FullName
                ActiveDocument.Shapes.Range(Array("myShape")).Select
                
                 With ActiveDocument.Shapes("myShape").Fill
                    .Visible = msoTrue
                    .UserPicture strName
                End With
            End If
        End With
         
         ' Clean up
        Set oDialog = Nothing
    End Sub
    Did it work? No, not quite. It tried to do something and it seemed to insert a picture but it came out as "The picture can't be displayed"

    I've fooled around with changing the code here and there but with no luck.

    I'm sure there must be an obvious mistake but cannot spot it.

    Any thoughts, please?

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    bump
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Sub test()
     FileName = "C:\Users\mdmac\OneDrive\Pictures\bear.jpg"
     With ActiveDocument.Shapes("MyShape").Fill
                    .Visible = msoTrue
                    .UserPicture FileName
                End With
    End Sub
    'or
    Sub test2()
     Set dlg = Application.FileDialog(FileDialogType:=1)
    dlg.Show
      FileName = dlg.SelectedItems(1)
     With ActiveDocument.Shapes("MyShape").Fill
        .Visible = msoTrue
        .UserPicture FileName
                End With
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    I think you were passing the wrong file name to .UserPicture



    Option Explicit
    Sub InsertPictureDialog()
        Dim oDialog         As Word.Dialog
        Dim strName As String
         
        Set oDialog = Dialogs(wdDialogInsertPicture)
        With oDialog
            .Display
            
             'Insert Shape Picture if the Name property (Filepath) <> ""
            If .Name <> "" Then
                With ActiveDocument.Shapes("myShape").Fill
                    .Visible = msoTrue
                    .UserPicture oDialog.Name   '   <<<<<<<<<<<<<<<<<
                End With
            End If
        End With
         
         ' Clean up
        Set oDialog = Nothing
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    Thanks again, folks for all your help.

    Applied the filename as you suggested, Paul, and it works brilliantly!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •