ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 135|回复: 2

[求助] 各位大佬有空帮忙看看哪里有错,插入的图片变形了

[复制链接]

TA的精华主题

TA的得分主题

发表于 2024-4-5 08:29 | 显示全部楼层 |阅读模式
Sub 插入图片()
    On Error Resume Next '有错误继续往下运行
    Application.ScreenUpdating = False
    Dim pth As String, brr
    Dim picNames As Variant
    Dim rng As Range
    Dim shp As Shape
    Dim fs As Object
    Dim folder As Object
    Dim file As Object
    pth = ThisWorkbook.Path & "\"
    ' 获取图片名称列表
    picNames = Array("作业证", "照片", "身份证", "查询证明")
    ' 删除已存在的图片
    For Each shp In Sheet1.Shapes
        If shp.Type = msoLinkedPicture Then shp.Delete
    Next
    ' 设置文件系统对象
    Set fs = CreateObject("Scripting.FileSystemObject")

    ' 循环插入图片
    For i = LBound(picNames) To UBound(picNames)
        ' 获取图片文件路径
        picPath = pth & picNames(i) & "\" & ListBox2.Text & ".jpg"
        ' 如果jpg格式的图片不存在,尝试png格式
        If Not fs.FileExists(picPath) Then
            picPath = pth & picNames(i) & "\" & ListBox2.Text & ".png"
        End If
        ' 如果找到了图片,插入到相应位置
        If fs.FileExists(picPath) Then
            Sheet1.Activate
            Set rng = Sheet1.Range(Array("A6", "G6", "A8", "G8")(i))
            With rng
                ' 插入图片
                Set shp = Sheet1.Shapes.AddPicture(picPath, msoFalse, msoCTrue, .Left, .Top, .Width, .Height)
                ' 设置图片属性
                Dim cellW As Single, cellH As Single    '单元格宽与高
                cellW = rng.MergeArea.Width  '获取单元格宽度
                cellH = rng.MergeArea.Height '获取单元格高度
                With shp
                    Dim picW As Single, picH As Single
                    Dim rtoW As Single, rtoH As Single
                    picW = .Width   '图片宽度
                    picH = .Height  '图片高度
                    rtoW = cellW / picW * 0.95   '重设图片的宽和高
                    rtoH = cellH / picH * 0.95
                    If rtoW < rtoH Then
                        .ShapeRange.ScaleWidth rtoW, msoFalse, msoScaleFromTopLeft
                    Else
                        .ShapeRange.ScaleHeight rtoH, msoFalse, msoScaleFromTopLeft
                    End If
                    picW = .Width
                    picH = .Height
                    .ShapeRange.LockAspectRatio = msoTrue          '锁定图片锁定纵横比
                    .Placement = xlMoveAndSize                     '图片的位置与大小随单元格变化而变化
                    .Left = .Left + (cellW - picW) / 2   '设置该图片的所在位置
                    .Top = .Top + (cellH - picH) / 2
                End With
            End With
        End If
    Next i
    [a1].Select
    Application.ScreenUpdating = True
End Sub

TA的精华主题

TA的得分主题

发表于 2024-4-5 13:14 | 显示全部楼层
没有附件不能测试代码。
建议上传Excel表格附件来说明问题。

TA的精华主题

TA的得分主题

发表于 2024-4-6 06:27 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
  1. .ShapeRange.LockAspectRatio = msoTrue    '调整图片尺寸之前,锁定图片锁定纵横比
  2. If rtoW < rtoH Then
  3.     .ShapeRange.ScaleWidth rtoW, msoFalse, msoScaleFromTopLeft
  4. Else
  5.     .ShapeRange.ScaleHeight rtoH, msoFalse, msoScaleFromTopLeft
  6. End If
  7. picW = .Width
  8. picH = .Height
复制代码
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-5-4 01:03 , Processed in 0.034825 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表