所有excelsheet的列不在pdf的相同页面; 同时使用Excel VBA进行转换

我试图用Excel VBA代码将具有大量列(70+)的Microsoft Excel文件转换为pdf。

在活动工作簿中,我试图将'Sheet1'保存为所需path的PDF格式。 我有以下代码。

Sub GetSaveAsFilename() Dim fileName As String fileName = Application.GetSaveAsFilename(InitialFileName:="", _ FileFilter:="PDF Files (*.pdf), *.pdf", _ Title:="Select Path and FileName to save") If fileName <> "False" Then With ActiveWorkbook .Worksheets("Sheet1").ExportAsFixedFormat Type:=xlTypePDF, fileName:= _ fileName, Quality:=xlQualityStandard, _ IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False End With End If End Sub 

当我运行VBA代码并保存PDF文件时,我看到了; 整个excelsheet不适合在同一页面。 它在下一页显示一些内容。

(只有less数列出现在第一页,剩余的出现在下一页等等。)。

我检查了如何发布PDF格式的宽工作表? 。

但是,将页面布局设置为横向并将Excel文件手动转换为PDF; 还会在下一页显示一些列。

有许多免费的Excel到PDF格式转换器在线,这给了我相同的结果。

VBA中是否有可用的function?通过这些function,我可以将PDF中的所有列放在一个页面中?

问题在于页面设置设置,我已经对代码做了一些小的修改,并添加了一个程序来执行页面设置设置,启动程序时可以select纸张大小,但请注意,允许的最小缩放比例是10% (请参阅PageSetup成员(Excel) ) 。 因此,如果即使在10%的打印区域不适合在一个页面,我build议select一个较大的纸张大小(即A3)生成一页PDF,然后在打印PDF时select适合页面。 该过程也给你改变边缘玩,当生成PDF的我设置所有边距为0,但你可以改变,因为它适合你的目标。

 Sub Wsh_LargePrintArea_To_Pdf() Dim WshTrg As Worksheet Dim sFileName As String sFileName = Application.GetSaveAsFilename( _ InitialFileName:="", _ FileFilter:="PDF Files (*.pdf), *.pdf", _ Title:="Select Path and FileName to save") If sFileName <> "False" Then Rem Set Worksheet Target Set WshTrg = ActiveWorkbook.Worksheets("Sheet1") Rem Procedure Update Worksheet Target Page Setup 'To Adjust the Page Setup Zoom select the Paper Size as per your requirements 'Call Wsh_Print_Setting_OnePage(WshTrg, xlPaperLetter) 'Call Wsh_Print_Setting_OnePage(WshTrg, xlPaperA4) 'To Adjust the Page Setup Zoom select the Paper Size as per your requirements 'If the Print Still don't fit in one page then use a the largest Paper Size (xlPaperA3) 'When printing the Pdf you can still selet to fix to the physical paper size of the printer. 'Call Wsh_Print_Setting_OnePage(WshTrg, xlPaperA3) 'This is the largest paper i can see in my laptop is 86.36 cm x 111.76 cm Call Wsh_Print_Setting_OnePage(WshTrg, xlPaperEsheet) Rem Export Wsh to Pdf WshTrg.ExportAsFixedFormat _ Type:=xlTypePDF, _ fileName:=sFileName, _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=False End If End Sub Sub Wsh_Print_Setting_OnePage(WshTrg As Worksheet, ePaperSize As XlPaperSize) On Error Resume Next Application.PrintCommunication = False With ActiveSheet.PageSetup .LeftMargin = Application.InchesToPoints(0) .RightMargin = Application.InchesToPoints(0) .TopMargin = Application.InchesToPoints(0) .BottomMargin = Application.InchesToPoints(0) .HeaderMargin = Application.InchesToPoints(0) .FooterMargin = Application.InchesToPoints(0) '.Orientation = xlLandscape .Orientation = xlPortrait .PaperSize = ePaperSize .Zoom = False .FitToPagesWide = 1 .FitToPagesTall = 1 End With Application.PrintCommunication = True End Sub 

将其添加到您的代码,它将强制所有打印在一张纸上,但仍然让它打印在多张纸高

 With Worksheets("Sheet1").PageSetup .FitToPagesWide = 1 .FitToPagesTall = False End With 

还将您的利润设置为“窄”

首先select您想要打印的范围,并将其设置为PrintArea。 然后运行这个代码,这个工作对我来说是79列表

 Sub saveAsPDF() Dim MyPath Dim MyFolder With Sheet1.PageSetup '.CenterHorizontally = True .CenterVertically = True .Orientation = xlLandscape .Zoom = False .FitToPagesWide = 1 .FitToPagesTall = 1 .BottomMargin = 0 .TopMargin = 0 .RightMargin = 0 .LeftMargin = 0 End With MyPath = ThisWorkbook.Path MyFolder = Application.GetSaveAsFilename(MyPath, "PDF Files (*.pdf),*.pdf") If MyFolder = False Then Exit Sub Sheet1.ExportAsFixedFormat Type:=xlTypePDF, _ Filename:=MyFolder, _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=False End Sub 

问题是你需要selectUsedRange然后使用Selection.ExportAsFixedFormat

 Sub GetSaveAsFilename() Dim fileName As String fileName = Application.GetSaveAsFilename(InitialFileName:="", _ FileFilter:="PDF Files (*.pdf), *.pdf", _ Title:="Select Path and FileName to save") If fileName <> "False" Then 'Selecting the Used Range in the Sheet ActiveWorkbook.Worksheets("Sheet1").UsedRange.Select 'Saving the Selection - Here is where the problem was Selection.ExportAsFixedFormat Type:=xlTypePDF, fileName:=fileName, _ Quality:=xlQualityStandard, IncludeDocProperties:=False, _ IgnorePrintAreas:=False, OpenAfterPublish:=True End If End Sub 

编辑:

问题在于PageSetup因为每个页面大小都有一个最大像素限制,因为您正在向您的评论前进。

“页面大小”设置为“超大A0”,这应该超过100×1500“使用UsedRange 。 在这里,您可以使用FitToPages... = 1更改页面大小,以检查您的Range是否在打印行内。

FitToPagesWideFitToPagesTall是将所有东西放在一个页面上。

 Sub GetSaveAsFilename() Dim fileName As String fileName = Application.GetSaveAsFilename(InitialFileName:="", _ FileFilter:="PDF Files (*.pdf), *.pdf", _ Title:="Select Path and FileName to save") If fileName <> "False" Then 'Suspending Communicaiton with Printer to Edit PageSetup via Scripting Application.PrintCommunication = False 'Setting Page Setup With ActiveSheet.PageSetup .FitToPagesWide = 1 .FitToPagesTall = 1 ' Setting Page Size to 92x92 inch Should cater for your data .PaperSize = 159 End With 'Enabling Communicaiton with Printer Application.PrintCommunication = True 'Selecting the Used Range in the Sheet ActiveWorkbook.Worksheets("Sheet1").UsedRange.Select 'Saving the Selection - Here is where the problem was Selection.ExportAsFixedFormat Type:=xlTypePDF, fileName:=fileName, _ Quality:=xlQualityStandard, IncludeDocProperties:=True, _ IgnorePrintAreas:=True, OpenAfterPublish:=True End If End Sub 

请注意,页面将显示为空白,您将需要放大以查看数据