输入单元格值后如何将 Excel 文档导出为 PDF?

How do you export an Excel document to PDF after cell value input?

参考文献:

OS:Windows 10 家企业

Excel Pro Plus 2016 - 32 位

文件类型 - xlsm

操作详情:

完成最后一个单元格输入框提示后,文档应自动将活动 sheet 导出为 PDF,无需保存文档。导出的 PDF 应具有 title 标签单元格和 pub 标签单元格给出的文档名称。

代码:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)


Dim rngC As Range
    Set rngC = Intersect(Target, Me.Range("Publication"))
    
Dim trig As Range
Dim title As String
Dim pub As String

   Set trig = Intersect(Target, Me.Range("OldDoc"))

    
    Select Case True
    

    Case Not rngC Is Nothing

Dim entryDate As String

         entryDate = Format$(Now, "ddmmmm,yyyy")

         On Error GoTo SafeExit
         Application.EnableEvents = False

Dim cellc As Range

         For Each cellc In rngC
             If Not IsEmpty(cellc) Then
                cellc.Offset(1).Value = entryDate
             End If
    Next


    
        Case Not trig Is Nothing
        
    title = Range("Title").Text
    pub = Range("Publication").Text
        
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Quality:=xlQualityStandard, _
                                    IncludeDocProperties:=True, IgnorePrintAreas:=False, _
                                    OpenAfterPublish:=True, Filename:=title & "-" & pub
        
End Select

    

SafeExit:
    Application.EnableEvents = True
    
    


End Sub

补充说明:

我已经包含了我的整个 worksheet_change 代码来展示我是如何安排所有流程的。

最后输入值的Cell定义如下:

Dim trig As Range

Set trig = Intersect(Target, Me.Range("OldDoc")) 'This will be the variable to trigger the export syntax

对于这个过程,我在代码中使用 Case 语句来确定哪个语句是正确的,然后执行该功能。所以我们从 Select Case True.

开始

为了使此过程的 case 语句为真,我们将其作为假阴性进行比较,因此当进行输入时,它将激活导出过程:

Case Not trig Is Nothing 'This is looking to see for changes in the variable, then will proceed to the process syntax in the statement

要删除在格式化为 PDF 之前保存文档的过程,ExportAsFixedFormat 的参数 OpenAfterPublish:=True。这将自动在 adobe Acrobat 或首选 PDF 软件中打开您的 excel 文档以继续编辑。

最终应该是这样的(当使用 case 语句格式时):

Dim trig As Range
Dim title As String
Dim pub As String

   Set trig = Intersect(Target, Me.Range("OldDoc"))

    
    Select Case True
        Case Not trig Is Nothing
        
    title = Range("Title").Text
    pub = Range("Publication").Text
        
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Quality:=xlQualityStandard, _
                                    IncludeDocProperties:=True, IgnorePrintAreas:=False, _
                                    OpenAfterPublish:=True, Filename:=title & "-" & pub
        
End Select

:

所有单元格范围都是指定的单元格标签,根据需要与首选单元格互换。请提供反馈,因为我仍在使用 vba 和正确的词汇进行开发。