I am trying to put comment background based on a certain field in the excel sheet. The field hold the filename of the .png file.
This is in Mac.
Here I have previously created a macro to add a picture in the comment:
Sub InsertPic()
'
' InsertPic Macro
'
' Keyboard Shortcut: Option+Cmd+p
strFileToOpen = Application.GetOpenFilename
MsgBox strFileToOpen
ActiveCell.AddComment
With ActiveCell.Comment.Shape
.ScaleWidth 5, msoFalse, msoScaleFromTopLeft
.ScaleHeight 5, msoFalse, msoScaleFromTopLeft
.Fill.UserPicture strFileToOpen
End With
End Sub
Here it opens a dialogue box with GetOpenFilename
but now I want to get the filenames in a loop. If I put one FileName in a string
strFileToOpen = "/path/FileName.png"
It gives out of memory! Any Clues?
UPDATE:
You need to add the reference Microsoft Scripting Runtime in order to use the variable type "Scripting.FileSystemObject".
This works for me
Sub InsertPic()
'
' InsertPic Macro
'
' Keyboard Shortcut: Option+Cmd+p
Dim FileExists As Variant
Dim FileSystemLibrary As New Scripting.FileSystemObject
On Error GoTo Err01InsertPic
Set FileExists = FileSystemLibrary.GetFile(ActiveCell.Value)
ActiveCell.AddComment
With ActiveCell.Comment.Shape
.ScaleWidth 5, msoFalse, msoScaleFromTopLeft
.ScaleHeight 5, msoFalse, msoScaleFromTopLeft
.Fill.UserPicture FileExists
End With
If 1 = 2 Then ' 99. If error
Err01InsertPic:
MsgBox ("Picture " & ActiveCell.Value & " doesn't exists!"), vbCritical
End If ' 99. If error
End Sub