Option Explicit
Option Private Module
Option Compare Binary
Option Base 0
#Const m_blnErrorHandlersOff_c = False
Public Sub Example()
If InsertPictureComment(ActiveSheet.Range("A1"), "C:\Test\PaleGreen.bmp", _
overWrite:=True) Then
MsgBox "Picture inserted"
Else
MsgBox "Could not insert picture."
End If
End Sub
Public Function InsertPictureComment( _
ByRef anchorRange As Excel.Range, _
ByVal imagePath As String, _
Optional ByVal imageHeight As Long = 90, _
Optional ByVal imageWidth As Long = 90, _
Optional ByVal overWrite As Boolean = False _
) As Boolean
Const strSpace_c As String = " "
Const lngOne_c As Long = 1
Dim strCmntName As String
Dim rng As Excel.Range
Dim cmnt As Excel.Comment
Dim shp As Excel.Shape
Dim xltbx As Excel.TextBox
Dim lngIndx As Long
Dim blnRtnVal As Boolean
#If Not m_blnErrorHandlersOff_c Then
On Error GoTo Err_Hnd
#End If
Set rng = anchorRange.Cells(lngOne_c, lngOne_c)
If overWrite Then
Set cmnt = rng.Comment
If Not cmnt Is Nothing Then
cmnt.Delete
End If
End If
Set cmnt = rng.AddComment(strSpace_c)
Set shp = cmnt.Shape
shp.Fill.UserPicture imagePath
shp.Height = imageHeight
shp.Width = imageWidth
On Error Resume Next
Do
Err.Clear
strCmntName = lngIndx & imagePath
shp.name = strCmntName
lngIndx = lngIndx + lngOne_c
Loop Until Not CBool(Err)
#If m_blnErrorHandlersOff_c Then
On Error GoTo 0
#Else
On Error GoTo Err_Hnd
#End If
Set xltbx = GetTextbox(rng.parent, strCmntName)
xltbx.ShapeRange.Shadow.Visible = False
blnRtnVal = True
Exit_Proc:
On Error Resume Next
Set rng = Nothing
Set cmnt = Nothing
Set shp = Nothing
Set xltbx = Nothing
InsertPictureComment = blnRtnVal
Exit Function
Err_Hnd:
blnRtnVal = False
MsgBox Err.Description, vbSystemModal, "Error: " & Err.Number
Resume Exit_Proc
Resume
End Function
Private Function GetTextbox( _
ByRef parent As Excel.Worksheet, _
ByVal name As String _
) As Excel.TextBox
Const lngLwrBnd_c As Long = 1
Dim xltbx As Excel.TextBox
Dim strName As String
Dim lngUprBnd As Long
Dim lngIndx As Long
#If Not m_blnErrorHandlersOff_c Then
On Error GoTo Err_Hnd
#End If
strName = LCase$(name)
lngUprBnd = parent.TextBoxes.Count
For lngIndx = lngLwrBnd_c To lngUprBnd
Set xltbx = parent.TextBoxes(lngIndx)
If LCase$(xltbx.name) = strName Then
Exit For
End If
Next
If lngIndx > lngUprBnd Then
Set GetTextbox = Nothing
End If
Exit_Proc:
On Error Resume Next
Set GetTextbox = xltbx
Set xltbx = Nothing
Exit Function
Err_Hnd:
Set xltbx = Nothing
Resume Exit_Proc
End Function
|