|
请测试:
- 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
- .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
复制代码
|
|