使用VBA将excel图导出为横向PDF

我有以下VBA代码基本上从Excel工作簿的工作表中的graphics并将其粘贴到PDF文档中:

Sub Graphics() Dim s As Workbook Dim ws As Worksheet, wsTemp As Worksheet Dim chrt As Shape Dim tp As Long Dim File As String Dim NewFileName As String Dim Path As String Application.DisplayAlerts = False Application.ScreenUpdating = False 'Here I define where the excel file is: SourcePath = "\\ukfs1\users\gabriem\Documents\Misproyectos\BigPromotions\QAPromo" 'Name of excel file that contains the graphs: File = "graphicator.xlsx" 'Open the excel file: Set s = Workbooks.Open(SourcePath & "\" & File) 'Name of the PDF I will create with the excel graphs: NewFileName = "\\ukfs1\users\gabriem\Documents\Mis proyectos\BigPromotions\QAPromo\test_pdf.pdf" 'Name of the excel sheet I want to export to PDF: Set ws = s.Sheets("Negocios") Set wsTemp = s.Sheets.Add tp = 2 ts = 5 'Copy-Pasting process: With wsTemp For Each chrt In ws.Shapes chrt.Copy wsTemp.Range("A1").PasteSpecial Selection.Top = tp Selection.Left = ts tp = tp + Selection.Height + 50 Next End With wsTemp.ExportAsFixedFormat Type:=xlTypePDF, Filename:=NewFileName, Quality:=xlQualityStandard, _ IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True wsTemp.Delete LetsContinue: With Application .ScreenUpdating = True .DisplayAlerts = True End With Exit Sub Whoa: MsgBox Err.Description Resume LetsContinue End Sub 

虽然代码工作正常,但它生成的输出不是graphics适合的,因为许多graphics都被剪切(其中一半是在另一页中)。 所以,我希望生成的PDF是水平的,但一直没能find答案。 从Excel不是一个问题,但从VBA代码创build新的PDF:

在这里输入图像说明 在这里输入图像说明

非常感谢你!!

我也有类似的问题,从图表生成PDF。 解决scheme是使用ChartObject而不是Shape ,并使用.CopyImage获取图表的图片,而不是试图复制实际的图表。 所以代码看起来更像:

 Dim chrt as ChartObject : For Each chrt in ws.ChartObjects chrt.CopyPicture wsTemp.Paste Selection.Top = tp Selection.Left = ts ts = ts + Selection.Width + 50 Next 

然后,您还应该在工作表中设置打印区域,并设置页面设​​置以将文档分页为您希望PDF显示的格式 – 使用横向格式(如果这就是“水平”的意思)。 你有更多的图表,将不适合在一个页面上? 如果是这样,您将需要检查累积宽度,以确保图表不会跨越页面边界。