|
|
- Sub 批量保存B列中的图片() '按A列名称导出B列图片
- Dim shp As Shape '定义shp为图形对象
- Dim pth As String '定义路径变量
- Dim N As Long
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- On Error Resume Next
- pth = ActiveWorkbook.Path & "" & "测试导出图片" & ""
- If Len(Dir(pth, vbDirectory)) = 0 Then MkDir pth
- N = 0
- For Each shp In ActiveSheet.Shapes '把当前表格里的每个图形赋值给shp
- If shp.Type = msoPicture Or shp.Type = msoLinkedPicture Or shp.Type = msoAutoShape Or shp.Type = msoChart Then
- Set Rng = shp.BottomRightCell '赋值RNG为图片所在的单元格
- If Rng.Row <= 600 And Rng.Column = 2 Then '设定图片范围600张以内,图片位于B列
- N = N + 1
- ' 直接导出图片,保持原始格式
- shp.Export pth & Range(Rng.Address).Offset(0, -1) & "." & GetPictureFormat(shp)
- End If
- End If
- Next
- MsgBox "图片共" & N & "张已导出到" & pth & "文件夹中!请前往查看:)"
- Application.ScreenUpdating = True
- Application.DisplayAlerts = True
- End Sub
-
- ' 辅助函数:获取图片格式
- Function GetPictureFormat(shp As Shape) As String
- Select Case shp.Type
- Case msoPicture, msoLinkedPicture
- On Error Resume Next
- Dim picFormat As String
- picFormat = shp.AlternativeText
- If InStr(1, picFormat, ".") > 0 Then
- GetPictureFormat = Mid(picFormat, InStrRev(picFormat, ".") + 1)
- Else
- GetPictureFormat = "png"
- End If
- On Error GoTo 0
- Case Else
- GetPictureFormat = "png"
- End Select
- End Function
复制代码
|
评分
-
1
查看全部评分
-
|