0

I am not a programmer so not sure what to do here. I would like an option of adding an image file in a Microsoft Word document userform for MAC. I had used a code earlier which works perfectly in Windows but it doesnt work for MAC and gives a 5948 error. I had added a field for the image in the userform with a button to add the image and the final submit button. The add button should allow the user to insert any size image from the local folder.

The code I was using is given below:

Dim ImagePath As String

Private Sub CMDAddImage_Click()

Dim objFileDialog As Office.FileDialog
    Set objFileDialog = Application.FileDialog(MsoFileDialogType.msoFileDialogFilePicker)

    With objFileDialog
        .AllowMultiSelect = False
        .ButtonName = "File Picker"
        .Title = "File Picker"
        If (.Show > 0) Then
        End If
        If (.SelectedItems.Count > 0) Then
            Call MsgBox(.SelectedItems(1))
            ImagePath = .SelectedItems(1)
        End If
    End With

Image1.Picture = LoadPicture(ImagePath)

End Sub

And the code in submit button was:

Dim objWord
Dim objDoc
Dim objShapes
Dim objSelection

'Set objSelection = ActiveDocument.Sections
'objSelection.TypeText (vbCrLf & "One Picture will be inserted here....")

ActiveDocument.Bookmarks("Field04").Select
   Set objShapes = ActiveDocument.InlineShapes

 objShapes.AddPicture (ImagePath)

End
End Sub

Can someone please help me edit the code for mac. In mac it does not allow to add the file.

enter image description here

enter image description here

braX
  • 11,506
  • 5
  • 20
  • 33

1 Answers1

0

You should check out the suggestion made by @JohnKorchok in a comment to your previous question - insert an image Content Control in your document instead, and throw away the VBA.

But if you need to keep using VBA and a UserForm...

Application.FileDialog is not available on Mac.

Application.GetOpenFileName is not avaialble from Word (it's an Excel thing).

Application.Dialogs does not do the same thing as GetOpenFileName so the user experience will be rather different, but at its simplest, you can use it like this:

With Application.Dialogs(wdDialogFileOpen)
  ' .Display = -1 for "OK" ("Open" in this case)
  ' .Display = 0 for "Cancel"
  ' (THere are other possible return values
  ' but I do not think they are applicable here)
  If .Display = -1 Then
    ImagePath = .Name
  End If
End With

or if you prefer, the lengthier

Dim dlg As Word.Dialog
Set dlg = Application.Dialogs(wdDialogFileOpen)
With dlg
  If .Display = -1 Then
    ImagePath = .Name
  End If
End With
Set dlg = Nothing

However, this dilaog does not let you specify file types or any kind of filtering, a starting folder etc. Attempts to set Finder search criteria via something like

.Name = "(_kMDItemFileName = ""*.jpg"")"
.Update

before the .Display either can't work or need different syntax.

Further, the Apple dialog may start with its own filtering set up so the user will have to click Options to enable All Files. You don't know what file type the user will choose so you will need to deal with that.

An alternative is to invoke Applescript. For this, it appears that you can still use the VBA MacScript command, which means that you can put all the script in your VBA file. If that does not work, then unfortunately you have to use AppleScriptTask which would require you to work some more on the Script and install the script in the correct folder on every Mac where you need this feature.

Here's the code I used - you would probably need to wrap everything up in another function call and use conditional compilation or other tests to call the correct routine depending on whether the code is running on Mac or Windows

Private Sub CMDAddImage_Click()
Dim s As String
Dim sFileName As String

On Error Resume Next
s = ""
' set this to some other location as appropriate
s = s & "set thePictureFoldersPath to (path to pictures folder)" & vbNewLine
s = s & "set applescript's text item delimiters to "",""" & vbNewLine
s = s & "set theFile to ¬" & vbNewLine
' add the image file types you want here
s = s & "(choose file of type {""png"",""jpg""} ¬" & vbNewLine
s = s & "with prompt ""Choose an image to insert."" ¬" & vbNewLine
s = s & "default location alias thePictureFoldersPath ¬" & vbNewLine
s = s & "multiple selections allowed false) as string" & vbNewLine
s = s & "set applescript's text item delimiters to """"" & vbNewLine
' choose file gives as an AFS path name (with colon delimiters)
' get one Word 2016/2019 will work with
s = s & "posix path of theFile"

sFileName = MacScript(s)

If sFileName <> "" Then
  ' Maybe do some more validation here
   ImagePath = sFileName
   Image1.Picture = LoadPicture(ImagePath)
End If
End Sub
  • Thank you. Trying it but can you help with code for submit button too or the one I have used is ok? – Hemangi Patil Jun 06 '20 at 19:28
  • @Hemangi Patil Surely the thing to do it is to try it for yourself? The code that you already have worked OK here for the images I chose. But I only tried it with a .jpg (and maybe a .png, I forget). Maybe not for all types of image. –  Jun 06 '20 at 21:06