我有一些VBA代码,我在网上从不同来源拼凑而成,将每个可打印区域导出为png。如果我运行VBA,它会输出文件,但作为空白白色图片。如果我在线上停下来
chartobj.Chart.Paste
然后它正确输出图片。
我尝试了几个循环或暂停,但除了手动停止该行的执行外没有任何作用。
Sub ExportToPNG()
' PNG Export Macro
' Change output = "C:\FOLDER\" to your folder path where you need the files saved
' Save Each Worksheet to a separate PNG file.
Dim ws As Worksheet
For Each ws In Worksheets
ws.Select
nm = ws.Name
Set sheet = ActiveSheet
output = "C:\FOLDER\" & nm & ".png"
zoom_coef = 100 / sheet.Parent.Windows(1).Zoom
Set area = sheet.Range(sheet.PageSetup.PrintArea)
area.CopyPicture xlPrinter
Set chartobj = sheet.ChartObjects.Add(0, 0, area.Width * zoom_coef,
area.Height * zoom_coef)
chartobj.Chart.Paste
chartobj.Chart.Export output, "png"
chartobj.Delete
Next ws
End Sub