View Single Post
 
Old 11-19-2021, 09:56 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Either see Customize the Office Ribbon (It doesn't take rocket science)

or even simpler, create a macro to show your userform in the document template and run that macro when a new document is created from the template. The macro enabled template would have the userform, the macro(s) the document text and (preferably) content controls placed to accept the data from the userform. Create a new document from the template.

The following macros should be saved in the template. Change the name of the form as appropriate. You can call the macro to fill the content control(s) as often as required from your userform code and the PDF macro to finish.
Code:
Option Explicit

Sub AutoNew()
    ShowMyForm
End Sub

Sub ShowMyForm()
    frmFormName.Show
End Sub

Private Sub FillCC(strCCTitle As String, strValue As String, Optional bLock As Boolean)
'Graham Mayor - https://www.gmayor.com - Last updated - 03 Sep 2021
Dim oCC As ContentControl
    On Error GoTo lbl_Exit
    For Each oCC In ActiveDocument.ContentControls
        If oCC.Title = strCCTitle Then
            oCC.LockContents = False
            oCC.Range.Text = strValue
            oCC.LockContentControl = True
            If bLock = True Then oCC.LockContents = True
            Exit For
        End If
    Next oCC
lbl_Exit:
    Set oCC = Nothing
    Exit Sub
End Sub

Private Sub SaveAsPDF()
Dim strDocName As String
Dim strPath As String
Dim intPos As Integer
Start:
    On Error Resume Next
    ActiveDocument.Save
    If ActiveDocument.path = "" Then
        MsgBox "Document must be saved first!", vbCritical, "Save Document"
        GoTo Start
    End If
    On Error GoTo 0
    strDocName = ActiveDocument.Name
    strPath = ActiveDocument.path & "\"
    intPos = InStrRev(strDocName, ".")
    If intPos = 0 Then
        ActiveDocument.Save
        GoTo Start
    End If
    strDocName = Left(strDocName, intPos - 1)
    strDocName = strPath & strDocName & ".pdf"

    ActiveDocument.ExportAsFixedFormat OutputFileName:=strDocName, _
                                       ExportFormat:=wdExportFormatPDF, _
                                       OpenAfterExport:=False, _
                                       OptimizeFor:=wdExportOptimizeForPrint, _
                                       Range:=wdExportAllDocument, From:=1, to:=1, _
                                       Item:=wdExportDocumentContent, _
                                       IncludeDocProps:=True, _
                                       KeepIRM:=True, _
                                       CreateBookmarks:=wdExportCreateHeadingBookmarks, _
                                       DocStructureTags:=True, _
                                       BitmapMissingFonts:=True, _
                                       UseISO19005_1:=False
lbl_Exit:
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote