使用图形将工作表另存为PDF

时间:2017-12-18 13:34:02

标签: excel vba excel-vba pdf

我正在尝试使用vba将我的工作表保存为pdf文件。工作表比A4页面宽,包含一些形状(从一侧到另一侧)。我希望工作表适合一个A4页面,因此它应该重新缩放,如屏幕上所示。我使用以下代码:

Sub Print_PDF()

Dim sFilename As String

Worksheets.Add.Name = "Helpsheet"
sFilename = "G:\anything\test.pdf"
ThisWorkbook.Worksheets("Newsletter").Range("A2:D81").CopyPicture xlScreen, xlBitmap

ThisWorkbook.Sheets("Helpsheet").Activate
ThisWorkbook.Sheets("Helpsheet").Paste
ActiveSheet.PageSetup.PrintArea = ThisWorkbook.Sheets("Helpsheet").Range("A1:O86")
With ActiveSheet.PageSetup
    .Orientation = xlPortrait
    .FitToPagesWide = 1
    .FitToPagesTall = 1
End With

ThisWorkbook.Sheets("Helpsheet").ExportAsFixedFormat Type:=xlTypePDF, Filename:=sFilename, quality:=xlQualityStandard, _
     IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True

Application.DisplayAlerts = False
ThisWorkbook.Sheets("Helpsheet").Delete
Application.DisplayAlerts = True

End Sub

代码实际上将PDF文件保存在预期的位置。但是,PDF文件是4页而不是预期的1页。因此,似乎没有正确定义printarea。我做错了什么?

2 个答案:

答案 0 :(得分:3)

您是否尝试过根据此示例调整PageSetup参数?

Query (hash = 07, y BETWEEN 113 AND 305) + Filter x >= 742
Query (hash = 08, y BETWEEN 113 AND 305)
Query (hash = 09, y BETWEEN 113 AND 305)
Query (hash = 10, y BETWEEN 113 AND 305) + Filter x <= 1082

明确地将.Zoom设置为false非常重要。

来源: excel vba not exporting pagesetup to pdf correctly

答案 1 :(得分:0)

假设活动表也是sheet("Helpsheet") 打印区域需要打印范围的地址。

ActiveSheet.PageSetup.PrintArea = ThisWorkbook.Sheets("Helpsheet").Range("A1:O86").Address