EXCEL批量插入批注图片,如果没有相对应的图片就不插入对应表格批注的办法!

2025-03-22 21:29:27
推荐回答(2个)
回答1:

Sub 批量插入同名照片到批注()
Dim a
a = MsgBox("【特别注意】:选中图片文件夹,保证图片文件名与插入批注表格的内容一样!", vbOKCancel, "【蓝迪资料员HM】")
Dim cell As Range, fd, t
Dim PicPath As String
Dim ErrCell As String
Selection.ClearComments
On Error Resume Next
Set fd = Application.FileDialog(msoFileDialogFolderPicker) '允许用户选择一个文件夹
If fd.Show = -1 Then
t = fd.SelectedItems(1) '选择之后就记录这个文件夹名称
End If
For Each cell In Selection
    If Dir(t & "\" & cell.Text & ".jpg", 16) <> Empty Then
        With cell.AddComment
        .Visible = True
        .Text Text:="cccc"
        .Shape.Select True
        Selection.ShapeRange.Fill.UserPicture t & "\" & cell.Text & ".jpg"
        .Shape.Width = 150 'Add these 2 statement
        .Shape.Height = 150
        .Visible = False
        End With
    End If
    cell.Offset(1, 0).Select
Next

End Sub

回答2:

判断一下,没有图片就不执行插入批注的代码