在Word中添加根据日期和内容保存为PDF

Adding Save to PDF Based on Date and Content in Word

我创建了一个 word 文档作为员工轮班清单。与我打交道的员工并不完全了解技术,所以这里的最终目标是易于使用。该表单目前由一些日期选择器和下拉列表内容控件组成。

这是我正在尝试做的事情:

我尝试了一些在堆栈中找到的简单方法,并找到了其他似乎适合 excel 的方法。但是我在实现所有功能时遇到了问题。

提前致谢!

编辑:

这是我找到并尝试过的代码。我不确定如何使用输入或路径修改文件名

Sub Convert_PDF()

 Dim desktoploc As String
 Dim filename As String
 Dim mypath As String

    desktoploc = CreateObject("WScript.Shell").SpecialFolders("Desktop")
    filename = ThisDocument.Name
    mypath = desktoploc & "\" & filename

    ActiveDocument.ExportAsFixedFormat OutputFileName:= _
        mypath, _
        ExportFormat:=wdExportFormatPDF, OpenAfterExport:=True, OptimizeFor:= _
        wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
        Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
        CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
        BitmapMissingFonts:=True, UseISO19005_1:=False
End Sub

我假设有两个 ContentControl。

一个带有名为“Date”的标签,一个带有名为“Shiftname”的标签

请注意 CC 标签区分大小写。

patternFilename:[date] 和 [shiftname] 将根据 CC 值替换

Option Explicit

'this goes into the Thisdocument-module

'adjust these consts to your needs
Private Const pathForReports As String = "D:\"
Private Const patternFilename As String = "Checklist [date] [shiftname].pdf"

Private Sub cmdSubmit_Click()
'ActiveX-Button on doc

    saveAsPdf

End Sub


Private Sub saveAsPdf()

Dim Fullfilename As String
Fullfilename = getFilename

ThisDocument.ExportAsFixedFormat OutputFileName:= _
        Fullfilename, _
        ExportFormat:=wdExportFormatPDF, OpenAfterExport:=True, OptimizeFor:= _
        wdExportOptimizeForPrint, Range:=wdExportAllDocument
End Sub

Private Function getFilename() As String

Dim strName As String
strName = patternFilename

strName = Replace(strName, "[date]", Format(getValueForCC("Date"), "yyyy-mm-dd"))
strName = Replace(strName, "[shiftname]", getValueForCC("Shiftname"))

getFilename = pathForReports & strName
End Function


Private Function getValueForCC(ccTag As String) As Variant
getValueForCC = ThisDocument.SelectContentControlsByTag(ccTag)(1).Range.Text
End Function

在文档中添加一个 ActiveX 按钮,右键单击它,选择属性并将其重命名为“cmdSubmit”: