I need your support to fix this code so that it doesn’t prompt for file access permissions in Excel for Mac.
The code works fine when the path directory is within my laptop’s local storage in the container folder in Excel, without prompting for file access permissions. However, when using iCloud storage or an external hard drive, it always prompts for access permissions for each photo.
Question: why do I save photos in iCloud or on an external hard drive instead of my laptop’s storage?
Answer: I have more photos than can fit on my laptop, which has only 512 GB of storage (MacBook Air M3). That’s why I transfer the photos to an external hard drive or iCloud to avoid overloading my laptop’s storage.
Here is the code:
Option Explicit
Sub UpdatePictures()
Dim R As Range
Dim S As Shape
Dim Path As String
Dim FName As String
Dim PicWidth As Double
Dim PicHeight As Double
Dim CellWidth As Double
Dim CellHeight As Double
Dim LeftPosition As Double
Dim TopPosition As Double
Dim AspectRatio As Double
' Define the path where images are stored
Path = "/Users/mac/Library/CloudStorage/OneDrive-Personal/Creative Cloud Files/ADD/"
If Right(Path, 1) <> "/" Then Path = Path & "/"
For Each R In Range("C2", Range("C" & Rows.Count).End(xlUp))
Set S = GetShapeByName(R.Value)
If S Is Nothing Then
' Check if the file exists
FName = Path & R.Value & ".jpg"
If Dir(FName) <> "" Then
' Insert picture
Set S = InsertPicturePrim(FName, R.Value)
End If
End If
If Not S Is Nothing Then
' Get picture dimensions
PicWidth = S.Width
PicHeight = S.Height
' Get cell dimensions
CellWidth = R.Offset(0, -1).Width
CellHeight = R.Offset(0, -1).Height
' Calculate aspect ratio
AspectRatio = PicWidth / PicHeight
' Set picture height to match the cell height
S.Height = CellHeight
' Calculate the new width based on the aspect ratio
S.Width = AspectRatio * CellHeight
' Calculate left position for center alignment
LeftPosition = R.Offset(0, -1).Left + (CellWidth - S.Width) / 2
' Calculate top position for center alignment
TopPosition = R.Offset(0, -1).Top + (CellHeight - S.Height) / 2
' Set picture position
S.Left = LeftPosition
S.Top = TopPosition
S.LockAspectRatio = msoTrue
Else
R.Offset(0, -1).Value = "NO PICTURE AVAILABLE"
End If
Next
End Sub
Private Function GetShapeByName(ByVal SName As String) As Shape
On Error Resume Next
Set GetShapeByName = ActiveSheet.Shapes(SName)
On Error GoTo 0
End Function
Private Function InsertPicturePrim(ByVal FName As String, ByVal SName As String) As Shape
Dim p As Shape
Static x As Integer
On Error Resume Next
x = x + 1
' Use the full path in the AddPicture method
Set p = ActiveSheet.Shapes.AddPicture(FName, msoFalse, msoCTrue, 1, 1, -1, -1)
If Not p Is Nothing Then
p.Name = SName & x
Set InsertPicturePrim = p
End If
On Error GoTo 0
End Function
I’m trying to find a way to display photos in Excel without prompting the grant file access
user26436678 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.