修改如下,照片文件夹路径可以通过对话框输入,也可将工作簿放在照片文件夹中,从而不必输入照片文件夹路径。代码通过预插入图片到单元格而获取图片尺寸,并将此用于批注框尺寸的设置。
Sub 插入批注图片()
Dim cell As Range, fd, t, w As Byte, h As Byte, Lj As String
Lj = InputBox("请输入JPG格式图片文件所在文件夹的路径:", , ThisWorkbook.Path) '获取路径,默认为当前文件夹路径
Selection.ClearComments
If Selection(1) = "" Then MsgBox "不能选择空白区。", 64, "提示": Exit Sub
On Error Resume Next
For Each cell In Selection
ActiveSheet.Pictures.Insert(Lj & "\" & cell.Text & ".jpg").Select
w = Selection.Width
h = Selection.Height
Selection.Delete
With cell.AddComment
.Visible = True
.Text Text:=""
.Shape.Select True
With Selection.ShapeRange
.LockAspectRatio = msoFalse
.Height = h * 3 '此处的3是指放大3倍显示,可自行调整
.Width = w * 3 '此处的3是指放大3倍显示,可自行调整
.LockAspectRatio = msoTrue
.Fill.UserPicture Lj & "\" & cell.Text & ".jpg"
End With
cell.Offset(1, 0).Select
.Visible = False
End With
Next
Exit Sub
End Sub
结果图如下:横向图片
纵向图片: