|
Sub OutputAllPic() '导出活动工作表(不一定是本工作簿中的工作表)里所有的JPG图片到...\桌面\OutputPic\
On Error GoTo ErrLabel
Application.ScreenUpdating = False
Dim theSh As Shell32.Shell
Dim X, Y, Z As Integer
Dim ExptPath, ItmNo As String
X = Val(InputBox("图片在第几列(填数字):", , 2))
Y = Val(InputBox("型号在第几列(填数字):", , 1))
Const theCUDesktop = 16
Set theSh = New Shell32.Shell
ExptPath = theSh.Namespace(theCUDesktop).Items.Item.Path & "\OutputPic\"
If Dir(ExptPath, 16) = "" Then '如果文件夹不存在
MkDir ExptPath '建立文件夹
End If
Z = 1
Do While True
ExptPath = theSh.Namespace(theCUDesktop).Items.Item.Path & "\OutputPic\" & Z '当前用户桌面上文件夹"OutputPic\1", "OutputPic\2".....
If Dir(ExptPath, 16) = "" Then '如果文件夹不存在
MkDir ExptPath '建立文件夹
Exit Do
End If
Z = Z + 1
Loop
For Each shp In ActiveSheet.Shapes
If shp.Type = 11 Or shp.Type = 13 And shp.TopLeftCell.Column = X Then
shp.Select
Dim P As Integer
P = shp.TopLeftCell.Row
Do While True
If Cells(P, 1).Top > shp.Top + shp.Height / 2 Then
Exit Do
End If
P = P + 1
Loop
P = P - 1
ItmNo = ActiveSheet.Cells(P, Y)
ItmNo = Replace(ItmNo, "\", "-")
ItmNo = Replace(ItmNo, "/", "-")
ItmNo = Replace(ItmNo, ":", "-")
ItmNo = Replace(ItmNo, "*", "x")
ItmNo = Replace(ItmNo, "?", "")
ItmNo = Replace(ItmNo, """", "in")
ItmNo = Replace(ItmNo, ">", "")
ItmNo = Replace(ItmNo, "<", "")
ItmNo = Replace(ItmNo, "|", "")
ItmNo = Replace(ItmNo, "+", "Plus")
ItmNo = Replace(ItmNo, "&", "-")
ItmNo = Trim(ItmNo)
Z = 1
Do While True
If Z > 1 Then
ItmNo = ItmNo & "(v" & Z & ")"
End If
FileYN = Dir(ExptPath & "\" & ItmNo & ".jpg")
If FileYN = "" Then
Exit Do
End If
Z = Z + 1
Loop
w1 = Selection.Width
h1 = Selection.Height
Selection.ShapeRange.ScaleWidth 1, msoTrue, msoScaleFromTopLeft
Selection.ShapeRange.ScaleHeight 1, msoTrue, msoScaleFromTopLeft
Selection.CopyPicture
w2 = Selection.Width
h2 = Selection.Height
Selection.Width = w1
Selection.Height = h1
With ActiveSheet.ChartObjects.Add(0, 0, w2, h2).Chart
.Paste
.Export ExptPath & "\" & ItmNo & ".jpg"
.Parent.Delete
End With
End If
Next
Application.ScreenUpdating = True
Exit_Label:
Exit Sub
ErrLabel:
MsgBox Err.Description
End Sub |
|