The below macro to insert a picture into a cell comment is functioning properly. The only issue is that it makes a copy of the picture in the folder of the workbook file path. Deleting this picture does not affect the inserted picture as a cell comment. How could the Kill command (or any other command) be used to delete this unneeded picture at the close of the macro? Thanks.
Code:
Option Explicit
Sub PictureIntoComment()
'Inserts picture into cell comment. Click active cell, then click picture, run macro.
Dim ch As ChartObject
Dim dWidth As Double
Dim dHeight As Double
Dim ws As Worksheet
Dim sName As String
Dim cmt As Comment
Dim sPath As String
Dim sFile As String
Dim rng As Range
Set ws = ActiveSheet
Set rng = ActiveCell
sPath = ThisWorkbook.Path & "\"
sName = InputBox("Name for picture file (no extension)", "File Name")
If sName = "" Then sName = "Picture_" & Format(Date, "ddmmyy")
sFile = sPath & sName & ".jpg"
dWidth = Selection.Width
dHeight = Selection.Height
ws.Unprotect "123"
Selection.Cut
Set ch = ws.ChartObjects.Add(Left:=rng.Left, Top:=rng.Top, _
Width:=dWidth, Height:=dHeight)
ch.Chart.Paste
rng.Activate
ch.Chart.Export sFile
ch.Delete
Set cmt = rng.AddComment
cmt.Text Text:=""
With cmt.Shape
.Fill.UserPicture sFile
.Width = dWidth
.Height = dHeight
End With
ws.Protect ("123"), DrawingObjects:=False, Contents:=True, Scenarios:= _
False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True, AllowInsertingRows:=True, AllowDeletingRows:= _
True, AllowSorting:=True, AllowFiltering:=True
End Sub