I have a Word 2010 template. I have a button at the top to automatically save the Word doc as a pdf with the (almost) correct name, in the correct directory, and open the doc.
I also have a Drop-Down Form Field to select the month.
The code for the button:
Private Sub CommandButton1_Click()
Convert_PDF
End Sub
Sub Convert_PDF()
Dim desktoploc As String
Dim filename As String
Dim date As String
Dim user As String
Dim mypath As String
desktoploc = CreateObject("WScript.Shell").SpecialFolders("Desktop")
filename = "Installs Team Metrics"
user = VBA.Environ("USERNAME")
mypath = desktoploc & "\Metrics\" & filename & " - " & date & " - " & user
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
This outputs the file to C:\Users[username]\Desktop\Metrics\Installs Team Metrics - [Month] - [username].pdf
How do I get what is selected in the dropdown box to the string "date".
Looks like a content control rather than a legacy field, so you would need
Activedocument.Contentcontrols(i).Range.Text
Where i is the index no. of the control (the first one in the document is 1, etc.)
So either you need to know the number, or you need to use the title or tag of the control (or something else) to identify the control. In the case of content controls, you then have to iterate the collection to find it, e.g.
Dim cc As ContentControl
For Each cc in ActiveDocument.ContentControls
If cc.Tag = "mytag" Then
strDate = cc.Range.Text
Exit For
End If
Next
Be careful how you title/tag your controls. Word does not enforce uniqueness of either titles or tags.
Another way to do this would be to create a Custom XML Part with a "Date" element and connect your control to it using xPath. Then you can retrieve the value directly from the Custom XML Part and you don't need to know anything about the control itself. But I think in a simple form that adds complications that you can do without.