工作的Excel 2013macros不工作在Excel 2016

我使用macros来生成发票的图像。 我们一直在使用Excel 2013,macros观工作完美无瑕。 但是,由于我们切换到Excel 2016macros未能生成有效的图像。 它创build一个空白图像。 它甚至不会抛出错误,也很难排除故障

Sub saveimage() Set Sheet = ActiveSheet output = "F:\Invoices\" & Range("e8") & ".png" zoom_coef = 100 / Sheet.Parent.Windows(1).Zoom Dim sht As Worksheet Dim LastRow As Long Dim LastColumn As Long Dim StartCell As Range Set sht = Worksheets("Invoice") Set StartCell = Range("A1") 'Refresh UsedRange Worksheets("Invoice").UsedRange 'Find Last Row LastRow = sht.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row 'Select Range sht.Range("A1:N" & LastRow).Select Set area = Selection 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 End Sub 

什么可能是问题的原因?

似乎有某种bug可以通过粘贴之前select过去的区域来解决。 所以,你应该添加下面一行

 chartobj.Chart.Parent.Select 

就在之前

 chartobj.Chart.Paste 

那么你将得到所需的输出文件。 只要确定我会在这里张贴你的Sub最后几行来说明这行必须被插入的位置:

 Set area = Selection 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 End Sub 

在下面的行中将.png更改为.jpg

 output = "F:\Invoices\" & Range("e8") & ".png" 

 chartobj.Chart.Export output, "png" 

Keashan