Excel vba 添加批注参考图_弹出选择窗口版.vba

19 篇文章 3 订阅
Sub 添加标注参考图_弹出选择窗口版()
'单选一个格子,打开选择文件窗口,选择图片确定。
'图片会入去作为批注背景图插入单元格。
'2015-07-06 笨笨
'上次等比缩放写错了,已经修正
'2015-11-10

On Error Resume Next

    Dim j_h, j_w, ww, initialPath
    Dim k '当前单元格
    
    Set k = Selection
    
    initialPath = ThisWorkbook.Path & "\images\"
    
    If Dir(initialPath, vbDirectory) = vbNullString Then
        initialPath = ThisWorkbook.Path
    End If
    
    
    '选择单一文件
    With Application.FileDialog(msoFileDialogFilePicker)
        .InitialFileName = initialPath
        .AllowMultiSelect = False '单选择,多选开关
        .Filters.Clear '清除文件过滤器
        .Filters.Add "图片", "*.png;*.jpg;*.bmp;*.gif" '设置两个文件过滤器,这是第一个
        .Filters.Add "所有文件", "*.*" '设置两个文件过滤器,这是第二个
    If .Show = -1 Then 'FileDialog 对象的 Show 方法显示对话框,并且返回 -1(如果您按 OK)和 0(如果您按 Cancel)。
        'MsgBox "您选择的文件是:" & .SelectedItems(1), vbOKOnly + vbInformation, "文件"
        filepath = .SelectedItems(1)
        Else
        GoTo gotoEnd
    End If
    
    End With
    
    '加载文件,看在不在。
    Filename = Dir(filepath)
    '加载图片,用于获取长宽。
    Set p = ActiveSheet.Pictures.Insert(filepath)
    
    '如果图片不存在,设置单元格为红色,结束此函数
    If p Is Nothing Then
        k.Interior.Color = RGB(192, 0, 0) '红色
        GoTo gotoEnd
    End If
    
    '这货他妈的不是像素是 点
    j_h = p.ShapeRange.Height
    j_w = p.ShapeRange.Width
    
    '如果图片大于 ww 等比缩放到 ww
    ww = 300
    If j_h > ww Then
    j_w = ww / j_h * j_w
    j_h = ww
    End If
    '取到长宽就删除图片,没有利用价值了
    ActiveSheet.Pictures.Delete
    
    With k
    '非空就可以插插了
    If Not k Is Nothing And Not Filename Is Nothing Then
        .ClearComments
        .AddComment
        .Comment.Shape.Fill.UserPicture PictureFile:=filepath
        .Comment.Shape.Height = j_h
        .Comment.Shape.Width = j_w
        '.Comment.Visible = True
        '.Interior.Color = RGB(155, 187, 89)
    End If
    End With
    
gotoEnd:

End Sub

参考资料

Docs 》Office VBA 参考 》Excel 》Excel Graph Visual Basic 参考 》方法 》UserPicture

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笑虾

多情黯叹痴情癫。情癫苦笑多情难

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值