Search code examples
activexcpu-wordcontentcontrol

Adding Save to PDF Based on Date and Content in Word


I have a word document created as a shift checklist for staff. The staff I'm dealing with isn't exactly up with technology, so the end game here is ease of use. The form is currently comprised of some Date Pickers and drop down list content controls.

Here's what I'm trying to do:

  • Set a simple Active X button as "Submit Form"
  • When Pressed, the file uses the date picker and drop down content control to format the file name as "Checklist date shift name " or some variation.
  • Saved as a PDF to a shared drive, I can correct the file path later.
  • Prefer to have the PDF open after save, sort of as a confirmation the action was completed.

I've tried some simple ones found here on stack, and found others that seem to be geared toward excel. But I'm having trouble implementing all the features.

Thanks in Advance!

Edit:

This is the code I found and tried. I'm not sure how to modify the file name using inputs or the path

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

Solution

  • I am assuming that there are two ContentControls.

    One with a tag named "Date", one with a tag named "Shiftname"

    Be aware that CC-tags are case-sensitive.

    patternFilename: [date] and [shiftname] will be replaced according to the CC-values

    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
    
    

    Add an ActiveX Button to the document, right click on it, choose properties and rename it to "cmdSubmit": enter image description here