如何在使用时创建PDF时自动选择保存按钮

时间:2017-06-29 06:16:39

标签: excel-vba pdf-generation vba excel

我的代码运行良好,并从工作表中创建PDF。但我想要这样做,而不必按文件路径的保存单选按钮。 或者是我需要创建一个确切的文件名并保存而无需选择

enter image description here

    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

    Set wbA = ActiveWorkbook
    Set wsA = ActiveSheet
    strTime = Format(Now(), "ddmmyyyy\_hhmm")

    strPath = wbA.Path
    If strPath = "" Then
      strPath = Application.DefaultFilePath
    End If
    strPath = strPath & "\"

    strName = Replace(wsA.Name, " ", "")
    strName = Replace(strName, ".", "_")

    strFile = strName & "_" & strTime & ".pdf"
    strPathFile = strPath & strFile

    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:
    CommandButton2.Visible = True
    CommandButton1.Visible = False     
    Exit Sub

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

2 个答案:

答案 0 :(得分:1)

在运行时创建一个确切的文件名并保存例如: -

ActiveSheet.ExportAsFixedFormat _ 
Type:=xlTypePDF _
Filename:= destfolder & PDF_Filename _
Quality:=xlQualityStandard _
IncludeDocProperties:=True _
IgnorePrintAreas:=False _
OpenAfterPublish:=True

答案 1 :(得分:1)

您还可以尝试以下方法:

$(document).ready(function () {
    getResult(1);
    $(document).on('click', '.pagination a', function () {
        //alert('hi ' + pageNum);
        var pageNum = $(this).text();
        getResult(pageNum);  
    });

    function getResult(pageNum) {
        $.getJSON('searchResult-' + pageNum + '.json', function (data) {
            var compileResult = Handlebars.compile($("#result").html());
            fullResult = compileResult(data);
            $("#results").html(fullResult);
        });
    }
});

以上内容向活动应用程序发送按键,这意味着Alt + S是Save ...的快捷方式。