ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[求助] 好几天了解决不了,请各位大神今天一定帮看看

[复制链接]

TA的精华主题

TA的得分主题

发表于 2023-12-26 19:35 | 显示全部楼层 |阅读模式
本帖最后由 howell2023 于 2023-12-26 20:28 编辑
  1. Sub outpic()
  2.   Dim strpath As String, strpicname As String, strwhere As String, strpicfullname As String
  3.   Dim shp As Shape, k As Long, d As Object, x As String, y As Long, numpic As Long
  4.   With Application.FileDialog(msoFileDialogFolderPicker)
  5.      If .Show Then strpath = .SelectedItems(1) Else: Exit Sub
  6.   End With
  7.   strwhere = InputBox("请输入图片名称相对应图片所在单元格的偏移位置,例如上1、下1、左1、右1", , "左1") '用户输入图片相对单元格的偏移位置。
  8.   If Len(strwhere) = 0 Then Exit Sub
  9.   x = Left(strwhere, 1) '偏移的方向
  10.   If InStr("上下左右", x) = 0 Then MsgBox "你未输入偏移方位!": Exit Sub
  11.   y = Val(Mid(strwhere, 2)) '偏移的值
  12.   Set d = CreateObject("scripting.dictionary")
  13.   Application.ScreenUpdating = False
  14. numpic = 0
  15. For Each shp In ActiveSheet.Shapes
  16.      If shp.Type = msoPicture Then
  17.          strpicname = getpicname(x, y, shp.TopLeftCell)
  18.          If Not d.exists(strpicname) Then
  19.             d(strpicname) = 1
  20.          Else
  21.             d(strpicname) = d(strpicname) + 1
  22.             strpicname = strpicname & d(strpicname)
  23.          End If
  24.          strpicfullname = strpath & "" & strpicname & ".jpg"
  25.          shp.Copy
  26.          numpic = numpic + 1
  27.         
  28.          With ActiveSheet.ChartObjects.Add(0, 0, shp.Width, shp.Height).Chart
  29.               .Parent.Select
  30.             
  31.               .Paste
  32.               .Export strpicfullname, "jpg"
  33.               .Parent.Delete
  34.          End With
  35.       End If
  36. Next
  37. Application.ScreenUpdating = True
  38. MsgBox "共导出" & numpic & "张图片" & Chr(13) & "路径:" & strpath, , "提示"

  39.   
  40.   
  41. End Sub
  42. Function getpicname(x As String, y As Long, rngshape As Range) As String
  43.     Dim strpicname As String
  44.     Select Case x
  45.         Case "上"
  46.         strpicname = rngshape.Offset(-y, 0).Value
  47.         Case "下"
  48.         strpicname = rngshape.Offset(y, 0)
  49.         Case "左"
  50.         strpicname = rngshape.Offset(0, -y)
  51.         Case "右"
  52.         strpicname = rngshape.Offset(0, y)
  53.     End Select
  54.     getpicname = IIf(strpicname = "", "图片", strpicname)

  55.    
  56. End Function
复制代码
好几天了解决不了,我要取图片的原始大小,然后保存,但现在是只取我缩小后的图片,逻辑是对的,但我想取原始大小的图片然后保存

测试用表.zip

1.66 MB, 下载次数: 4

TA的精华主题

TA的得分主题

发表于 2023-12-27 09:40 | 显示全部楼层


请测试:

  1. Sub outpic()
  2.     Dim strpath As String, strpicname As String, strwhere As String, strpicfullname As String
  3.     Dim shp As Shape, k As Long, d As Object, x As String, y As Long, numpic As Long
  4.     With Application.FileDialog(msoFileDialogFolderPicker)
  5.         If .Show Then strpath = .SelectedItems(1) Else: Exit Sub
  6.     End With
  7.     strwhere = InputBox("请输入图片名称相对应图片所在单元格的偏移位置,例如上1、下1、左1、右1", , "左1") '用户输入图片相对单元格的偏移位置。
  8.     If Len(strwhere) = 0 Then Exit Sub
  9.     x = Left(strwhere, 1) '偏移的方向
  10.     If InStr("上下左右", x) = 0 Then MsgBox "你未输入偏移方位!": Exit Sub
  11.     y = Val(Mid(strwhere, 2)) '偏移的值
  12.     Set d = CreateObject("scripting.dictionary")
  13.     Application.ScreenUpdating = False
  14.     numpic = 0
  15.     For Each shp In ActiveSheet.Shapes
  16.         If shp.Type = msoPicture Then
  17.             strpicname = getpicname(x, y, shp.TopLeftCell)
  18.             If Not d.exists(strpicname) Then
  19.                 d(strpicname) = 1
  20.             Else
  21.                 d(strpicname) = d(strpicname) + 1
  22.                 strpicname = strpicname & d(strpicname)
  23.             End If
  24.             strpicfullname = strpath & "" & strpicname & ".jpg"
  25.             numpic = numpic + 1
  26.             H = shp.Height
  27.             W = shp.Width
  28.             shp.ScaleHeight 1#, msoTrue, msoScaleFromTopLeft
  29.             shp.ScaleWidth 1#, msoTrue, msoScaleFromTopLeft
  30.             shp.CopyPicture
  31.             With ActiveSheet.ChartObjects.Add(0, 0, shp.Width, shp.Height).Chart
  32.                 .Paste
  33.                 .Export strpicfullname, "jpg"
  34.                 .Parent.Delete
  35.             End With
  36.             shp.Height = H
  37.             shp.Width = W
  38.         End If
  39.     Next
  40.     Application.ScreenUpdating = True
  41.     MsgBox "共导出" & numpic & "张图片" & Chr(13) & "路径:" & strpath, , "提示"
  42. End Sub
  43. Function getpicname(x As String, y As Long, rngshape As Range) As String
  44.     Dim strpicname As String
  45.     Select Case x
  46.     Case "上"
  47.         strpicname = rngshape.Offset(-y, 0).Value
  48.     Case "下"
  49.         strpicname = rngshape.Offset(y, 0)
  50.     Case "左"
  51.         strpicname = rngshape.Offset(0, -y)
  52.     Case "右"
  53.         strpicname = rngshape.Offset(0, y)
  54.     End Select
  55.     getpicname = IIf(strpicname = "", "图片", strpicname)
  56. End Function
复制代码


TA的精华主题

TA的得分主题

 楼主| 发表于 2023-12-30 11:28 | 显示全部楼层
Sub outpic()     Dim strpath As String, strpicname As String, strwhere As String, strpicfullname As String     Dim shp As Shape, k As Long, d As Object, x As String, y As Long, numpic As Long     With Application.FileDialog(msoFileDialogFolderPicker)         If .Show Then strpath = .SelectedItems(1) Else: Exit Sub     End With     strwhere = InputBox("请输入图片名称相对应图片所在单元格的偏移位置,例如上1、下1、左1、右1", , "左1") '用户输入图片相对单元格的偏移位置。     If Len(strwhere) = 0 Then Exit Sub     x = Left(strwhere, 1) '偏移的方向     If InStr("上下左右", x) = 0 Then MsgBox "你未输入偏移方位!": Exit Sub     y = Val(Mid(strwhere, 2)) '偏移的值     Set d = CreateObject("scripting.dictionary")     Application.ScreenUpdating = False     numpic = 0     For Each shp In ActiveSheet.Shapes         If shp.Type = msoPicture Then             strpicname = getpicname(x, y, shp.TopLeftCell)             If Not d.exists(strpicname) Then                 d(strpicname) = 1             Else                 d(strpicname) = d(strpicname) + 1                 strpicname = strpicname & d(strpicname)             End If             strpicfullname = strpath & "\" & strpicname & ".jpg"             numpic = numpic + 1             H = shp.Height             W = shp.Width             shp.ScaleHeight 1#, msoTrue, msoScaleFromTopLeft             shp.ScaleWidth 1#, msoTrue, msoScaleFromTopLeft             shp.CopyPicture             With ActiveSheet.ChartObjects.Add(0, 0, shp.Width, shp.Height).Chart                 .Parent.Select                 .Paste                 .Export strpicfullname, "jpg"                 .Parent.Delete             End With             shp.Height = H             shp.Width = W         End If     Next     Application.ScreenUpdating = True     MsgBox "共导出" & numpic & "张图片" & Chr(13) & "路径:" & strpath, , "提示" End Sub Function getpicname(x As String, y As Long, rngshape As Range) As String     Dim strpicname As String     Select Case x     Case "上"         strpicname = rngshape.Offset(-y, 0).Value     Case "下"         strpicname = rngshape.Offset(y, 0)     Case "左"         strpicname = rngshape.Offset(0, -y)     Case "右"         strpicname = rngshape.Offset(0, y)     End Select     getpicname = IIf(strpicname = "", "图片", strpicname) End Function

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-12-30 11:29 | 显示全部楼层
  1. Sub outpic()
  2.     Dim strpath As String, strpicname As String, strwhere As String, strpicfullname As String
  3.     Dim shp As Shape, k As Long, d As Object, x As String, y As Long, numpic As Long
  4.     With Application.FileDialog(msoFileDialogFolderPicker)
  5.         If .Show Then strpath = .SelectedItems(1) Else: Exit Sub
  6.     End With
  7.     strwhere = InputBox("请输入图片名称相对应图片所在单元格的偏移位置,例如上1、下1、左1、右1", , "左1") '用户输入图片相对单元格的偏移位置。
  8.     If Len(strwhere) = 0 Then Exit Sub
  9.     x = Left(strwhere, 1) '偏移的方向
  10.     If InStr("上下左右", x) = 0 Then MsgBox "你未输入偏移方位!": Exit Sub
  11.     y = Val(Mid(strwhere, 2)) '偏移的值
  12.     Set d = CreateObject("scripting.dictionary")
  13.     Application.ScreenUpdating = False
  14.     numpic = 0
  15.     For Each shp In ActiveSheet.Shapes
  16.         If shp.Type = msoPicture Then
  17.             strpicname = getpicname(x, y, shp.TopLeftCell)
  18.             If Not d.exists(strpicname) Then
  19.                 d(strpicname) = 1
  20.             Else
  21.                 d(strpicname) = d(strpicname) + 1
  22.                 strpicname = strpicname & d(strpicname)
  23.             End If
  24.             strpicfullname = strpath & "" & strpicname & ".jpg"
  25.             numpic = numpic + 1
  26.             H = shp.Height
  27.             W = shp.Width
  28.             shp.ScaleHeight 1#, msoTrue, msoScaleFromTopLeft
  29.             shp.ScaleWidth 1#, msoTrue, msoScaleFromTopLeft
  30.             shp.CopyPicture
  31.             With ActiveSheet.ChartObjects.Add(0, 0, shp.Width, shp.Height).Chart
  32.                 .Parent.Select
  33.                 .Paste
  34.                 .Export strpicfullname, "jpg"
  35.                 .Parent.Delete
  36.             End With
  37.             shp.Height = H
  38.             shp.Width = W
  39.         End If
  40.     Next
  41.     Application.ScreenUpdating = True
  42.     MsgBox "共导出" & numpic & "张图片" & Chr(13) & "路径:" & strpath, , "提示"
  43. End Sub
  44. Function getpicname(x As String, y As Long, rngshape As Range) As String
  45.     Dim strpicname As String
  46.     Select Case x
  47.     Case "上"
  48.         strpicname = rngshape.Offset(-y, 0).Value
  49.     Case "下"
  50.         strpicname = rngshape.Offset(y, 0)
  51.     Case "左"
  52.         strpicname = rngshape.Offset(0, -y)
  53.     Case "右"
  54.         strpicname = rngshape.Offset(0, y)
  55.     End Select
  56.     getpicname = IIf(strpicname = "", "图片", strpicname)
  57. End Function
  58. 最终修改后完整的,终于解决了,谢谢lujkhua
复制代码
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-11-19 15:33 , Processed in 0.040251 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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