Microsoft Excel

Ron de Bruin
Excel Automation

Microsoft MVP Program

Insert Picture on Worksheet with VBA in Mac Office

When you insert pictures into your worksheet in Mac Excel with VBA you will notice that the location of the pictures on your Mac can be important because of Apple's sandbox requirements, read more on this page :

Problems with Apple’s sandbox requirements and Mac Office 2016 and up with VBA code

Examples

Basic example that let you browse to the picture and insert it in cel A8, seems to have no problem with Apple's sandbox requirements and is working.

Sub BrowseInsertPicture()
    Dim NamePathPicture As Variant
    Dim img As Picture
    NamePathPicture = Application.GetOpenFilename(Title:="Select the picture that you want to insert")
    If NamePathPicture = False Then Exit Sub
    Set img = ActiveSheet.Pictures.Insert(NamePathPicture)
    With img
        .Left = ActiveSheet.Range("A8").Left
        .Top = ActiveSheet.Range("A8").Top
        .Width = 200
        .Height = 150
        .Placement = 1
        .PrintObject = True
    End With
End Sub

But if we want to use the path from a picture that is in a cell A1 like : /Users/rondebruin/Desktop/test1.png

Sub InsertPicture()
    Dim NamePathPicture As String
    Dim img As Picture
    NamePathPicture = Sheets(1).Range("A1").Value
    Set img = ActiveSheet.Pictures.Insert(NamePathPicture)
    With img
        .Left = ActiveSheet.Range("A8").Left
        .Top = ActiveSheet.Range("A8").Top
        .Width = 200
        .Height = 150
        .Placement = 1
        .PrintObject = True
    End With
End Sub

You will notice that it will display the "cannot be displayed box" instead of the picture, if you insert the picture manual one time the code will work but that is no solution.

To make this work you must use a folder for your pictures that not have a problem with Apple's sandbox requirements, in the article on top of this page you can read how you can create a folder in the correct location or you can use the script on this page to make it for you and also make a shortcut to the folder on your desktop : Setup your Mac for Mac Office 2016 or higher

The path not looks like this anymore  /Users/rondebruin/Desktop/test1.png

But something like this :

/Users/rondebruin/Library/Group Containers/UBF8T346G9.Office/MacOffice2016Files/test1.png