将Excel工作表导出为pdf

时间:2018-10-17 06:04:23

标签: excel pdf export

我目前正在使用Excel Makro进行工作,并且其中一个命令Buttons应该将用户窗体中的输入内容复制到工作簿的工作表中,并将该工作表导出为pdf。宏工作正常(因为我在Internet上找到了大多数宏),但是一旦用户在“文件另存为”窗口中单击“取消”按钮,宏就会停止运行。

我的问题是,该宏仍然保存工作表,并将pdf称为“ False.pdf”。谁能帮助我弄清楚我应该进行哪些更改才能使其正常运行?

我使用的代码是:

Private Sub CommandButton3_Click()
    Dim wsA As Worksheet
    Dim wbA As Workbook
    Dim strTime As String
    Dim strName As String
    Dim strPath As String
    Dim strFile As String
    Dim strPathFile As String
    Dim myFile As Variant
    On Error GoTo errHandler

    ThisWorkbook.Worksheets("Export").Range("B2").Value = Label14.Caption
    ThisWorkbook.Worksheets("Export").Range("B4").Value = Label12.Caption
    ThisWorkbook.Worksheets("Export").Range("K4").Value = Paketdruck.Value
    ThisWorkbook.Worksheets("Export").Range("H2").Value = Label16.Caption
    ThisWorkbook.Worksheets("Export").Range("J2").Value = Label18.Caption
    ThisWorkbook.Worksheets("Export").Range("G4").Value = Label22.Caption
    ThisWorkbook.Worksheets("Export").Range("B17").Value = Label37.Caption
    ThisWorkbook.Worksheets("Export").Range("B19").Value = Label38.Caption
    ThisWorkbook.Worksheets("Export").Range("B21").Value = Label39.Caption
    ThisWorkbook.Worksheets("Export").Range("C17").Value = Label42.Caption
    ThisWorkbook.Worksheets("Export").Range("C19").Value = Label43.Caption
    ThisWorkbook.Worksheets("Export").Range("C21").Value = Label44.Caption
    ThisWorkbook.Worksheets("Export").Range("E17").Value = Label46.Caption
    ThisWorkbook.Worksheets("Export").Range("E19").Value = Label47.Caption
    ThisWorkbook.Worksheets("Export").Range("E21").Value = Label48.Caption
    ThisWorkbook.Worksheets("Export").Range("G17").Value = Label50.Caption
    ThisWorkbook.Worksheets("Export").Range("G19").Value = Label51.Caption
    ThisWorkbook.Worksheets("Export").Range("G21").Value = Label52.Caption
    ThisWorkbook.Worksheets("Export").CheckBox1.Value = Me.CheckBox1.Value
    ThisWorkbook.Worksheets("Export").CheckBox2.Value = Me.CheckBox2.Value
    ThisWorkbook.Worksheets("Export").CheckBox3.Value = Me.CheckBox3.Value
    ThisWorkbook.Worksheets("Export").CheckBox4.Value = Me.CheckBox4.Value
    ThisWorkbook.Worksheets("Export").CheckBox5.Value = Me.CheckBox5.Value

    Set wbA = ActiveWorkbook
    Set wsA = ThisWorkbook.Worksheets("Export")
    strTime = Format(Now(), "yyyymmdd")

    'get active workbook folder, if saved
    strPath = wbA.Path
    If strPath = "" Then
        strPath = Application.DefaultFilePath
    End If
    strPath = strPath & "\"

    'create default name for savng file
    strName = wsA.Name
    strFile = strName & "_" & strTime & ".pdf"
    strPathFile = strPath & strFile

    'user can enter name and
    ' select folder for file
    myFile = Application.GetSaveAsFilename _
        (InitialFileName:=strPathFile, _
            FileFilter:="PDF Files (*.pdf), *.pdf", _
            Title:="Select Folder and FileName to save")

    'export to PDF if a folder was selected
    If myFile <> "False" Then
        wsA.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=myFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
    'confirmation message with file info
    MsgBox "PDF file has been created: " _
      & vbCrLf _
      & myFile
    End If

    exitHandler:
        Exit Sub
    errHandler:
        MsgBox "Could not create PDF file"
        Resume exitHandler
End Sub

感谢您的帮助:)

编辑,我发现了问题所在。 我必须更改“ myFile”变量的结果,因为我使用的是德国Excel许可证,因此必须检查

 If myFile <> "Falsch" Then
 Code
 End If

感谢@SolarMike的帮助

0 个答案:

没有答案