|
看了好多贴子,终于找到解决导出图片为空白的办法了
".paste上面插入一句.Parent.select就可以解决导出图片空白的问题。"
Sub PicOutput()
OpenFile = Application.GetOpenFilename("请选择任一文件后按确定(*.*),*.*", , "选择任一文件确定图片输出文件夹,或取消获得当前文件所在文件夹。")
If OpenFile = False Then
myDir = ThisWorkbook.Path & "\"
Else
myDir = Left(OpenFile, InStrRev(OpenFile, "\"))
End If
k = InputBox("1=列左,2=列右,3=上一行,4=下一行,取消=图片所在单元格或无名称", "选择图片名称位置:", 2)
If k = 1 Then
r = 0: c = -1
ElseIf k = 2 Then
r = 0: c = 1
ElseIf k = 3 Then
r = -1: c = 0
ElseIf k = 4 Then
r = 1: c = 0
End If
k = MsgBox("Yes=按原尺寸,No=按新设定,Cancel=按现在显示", vbYesNoCancel, "输出图片尺寸大小选择")
For Each p In ActiveSheet.Shapes
ph = p.Height
pw = p.Width
On Error Resume Next
pn = p.TopLeftCell.Offset(r, c).Value
If Err.Number <> 0 Then Err.Clear
If pn = "" Then n = n + 1: pn = Left(ThisWorkbook.Name, InStrRev(ThisWorkbook.Name, ".") - 1) & Format(n, "000")
GetPicName:
On Error Resume Next
p.Name = pn & ".jpg"
If Err.Number <> 0 Then
Err.Clear
pn = InputBox("图片名称重复,请重新命名", "图片名称重复", pn)
GoTo GetPicName
End If
p.Select
If k = vbYes Then
Selection.Copy
ActiveSheet.PasteSpecial Format:="图片 (JPEG)" '英文版为"Picture (JPEG)"
Selection.Name = "myPic"
ElseIf k = vbNo Then
Selection.ShapeRange.LockAspectRatio = msoFalse
f = InputBox("放大缩小比率", "图片尺寸设定", 2)
If IsNumeric(f) And f > 0 Then
Selection.ShapeRange.Height = ph * f
Selection.ShapeRange.Width = pw * f
Else
Selection.ShapeRange.Height = InputBox("重新设定图片高度", "图片高尺寸设定", ph)
Selection.ShapeRange.Width = InputBox("重新设定图片宽度", "图片宽尺寸设定", pw)
End If
End If
Selection.CopyPicture
With ActiveSheet.ChartObjects.Add(0, 0, Selection.Width + 5, Selection.Height + 5).Chart
.Parent.Select
.Paste
.Export myDir & p.Name, "JPG"
.Parent.Delete
End With
If k = vbYes Then
ActiveSheet.Shapes("myPic").Delete
ElseIf k = vbNo Then
p.Height = ph
p.Width = pw
End If
p.TopLeftCell.Offset(, -1).Select
Next
End Sub
|
|