none
Import an image macro RRS feed

  • Question

  • I'm trying to create a word macro that will prompt the user to look for an image and then import it. However it doesn't recognize GetOpenFileName ... the code is below. Any suggestions?

    Sub ImportImage()
    '
    ' ImportImage Macro
    '
    '
        Dim strFileName As String
        strFileName = Application.GetOpenFilename
        If strFileName = "False" Then Exit Sub
        Selection.InlineShapes.AddPicture FileName:= _
            strFileName, LinkToFile:=False, _
            SaveWithDocument:=True
    End Sub

    Wednesday, May 1, 2013 6:09 PM

Answers

  • Application.GetOpenFilename is Excel VBA, not Word VBA. You can use code like this instead:

    Sub ImportImage()
        Dim strFile As String
        With Application.FileDialog(1) ' msoFileDialogOpen
            .Filters.Clear
            .Filters.Add "Image files", "*.bmp;*.gif;*.jpg;*.png"
            If .Show Then
                strFile = .SelectedItems(1)
                Selection.InlineShapes.AddPicture FileName:=strFile, _
                    LinkToFile:=False, SaveWithDocument:=True
            Else
                MsgBox "No image file selected!", vbExclamation
            End If
        End With
    End Sub


    Regards, Hans Vogelaar

    • Marked as answer by MikeGillard Wednesday, May 1, 2013 6:31 PM
    Wednesday, May 1, 2013 6:20 PM

All replies

  • Application.GetOpenFilename is Excel VBA, not Word VBA. You can use code like this instead:

    Sub ImportImage()
        Dim strFile As String
        With Application.FileDialog(1) ' msoFileDialogOpen
            .Filters.Clear
            .Filters.Add "Image files", "*.bmp;*.gif;*.jpg;*.png"
            If .Show Then
                strFile = .SelectedItems(1)
                Selection.InlineShapes.AddPicture FileName:=strFile, _
                    LinkToFile:=False, SaveWithDocument:=True
            Else
                MsgBox "No image file selected!", vbExclamation
            End If
        End With
    End Sub


    Regards, Hans Vogelaar

    • Marked as answer by MikeGillard Wednesday, May 1, 2013 6:31 PM
    Wednesday, May 1, 2013 6:20 PM
  • Thanks Hans,

    That did it.

    Wednesday, May 1, 2013 6:31 PM