繁体   English   中英

将Excel工作表导出为pdf

[英]Exporting Excel sheet to pdf

我目前正在使用Excel Makro进行工作,并且其中一个命令Buttons应该将输入从UserForm复制到我的工作簿的工作表中,并将该工作表导出为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的帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM