Option Explicit
Private Declare Function SetTimer Lib "user32" _
(ByVal hwnd As Long, _
ByVal nIDEvent As Long, _
ByVal uElapse As Long, _
ByVal lpTimerFunc As Long) As Long
Private Declare Function KillTimer Lib "user32" _
(ByVal hwnd As Long, _
ByVal nIDEvent As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
pDest As Any, _
pSource As Any, _
ByVal dwLength As Long)
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" ( _
ByVal lpString As Long) As Long
Private bPictureAdded As Boolean
Public Function GetPicture( _
ByVal ImageFilePathName As String _
) As Boolean
Dim lPtr As Long
Dim sImageAndFileNames As String
Dim oPic As Object
On Error Resume Next
Application.Caller.Parent.Shapes(Application.Caller.Address(, , , True)).Delete
bPictureAdded = False
If Len(ImageFilePathName) > 0 Then
If Len(Dir(ImageFilePathName)) > 0 Then
Set oPic = LoadPicture(ImageFilePathName)
If Not oPic Is Nothing Then
sImageAndFileNames = Application.Caller.Address(, , , True) & "|" & ImageFilePathName
lPtr = StrPtr(sImageAndFileNames)
SetTimer Application.hwnd, lPtr, 1, AddressOf AddPicture
Do
DoEvents
Loop Until bPictureAdded
GetPicture = True
End If
Set oPic = Nothing
End If
End If
End Function
Private Sub AddPicture( _
ByVal hwnd As Long, _
ByVal uMsg As Long, _
ByVal nIDEvent As Long, _
ByVal dwTimer As Long _
)
Dim sImageAndFileNames As String
Dim sTemp As String
Dim lLen As Long
Dim oPic As Shape
Dim ar() As String
On Error Resume Next
KillTimer Application.hwnd, nIDEvent
lLen = lstrlenW(nIDEvent) * 2
sTemp = Space(lLen)
CopyMemory ByVal sTemp, ByVal nIDEvent, lLen
sImageAndFileNames = Replace(sTemp, Chr(0), "")
ar = Split(sImageAndFileNames, "|")
With Range(ar(0))
.Parent.Shapes(ar(0)).Delete
Set oPic = .Parent.Shapes.AddPicture _
(ar(1), msoCTrue, msoFalse, .Left, .Top, .Width, .Height)
End With
With oPic
.Placement = xlFreeFloating
.Name = ar(0)
.Visible = msoCTrue
End With
Set oPic = Nothing
Erase ar
bPictureAdded = True
End Sub