如何使用VBA将工作簿中的特定工作表保存为唯一的PDF文件

时间:2017-01-29 15:17:39

标签: excel-vba vba excel

我正在尝试使用以下代码将特定工作表另存为PDF,但错误信息显示为Run Time Error'1004': Method 'SaveAs' of object '_Worksheet' Failed

Sub SaveAsPDF()
    Dim path As String
    Dim MyDate As String
    Dim WS As Worksheet
    path = "c:\invoice\"

    MyDate = Date
    MyDate = Format(MyDate, "dd_mm_yyyy")

    Application.DisplayAlerts = False

    For Each WS In ThisWorkbook.Worksheets
        WS.SaveAs Filename:=path & Range("D6").Text & "-" & Range("K6").Value & "-" & _
        MyDate, FileFormat:=xlTypePDF
    Next

    Application.DisplayAlerts = True
    ActiveWorkbook.Close savechanges:=False
End Sub 

1 个答案:

答案 0 :(得分:0)

您需要使用HORIZONTAL = false,而不是ExportAsFixedFormat(以及相应的参数名称)

SaveAs