View Single Post
 
Old 01-30-2021, 05:22 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
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

OK It seems that you are using content controls, but you will need to title them to which end Insert Content Control Add-In makes things easier.
The code to save the form using data from the fields is then relatively simple e.g. as follows. This assumes three controls titled Date, Time and Number (you can add more as required) The code is aware of the possibility of illegal filename characters, which it replaces with underscores,
Code:
Sub SaveForm()
'Graham Mayor - https://www.gmayor.com - Last updated - 30 Jan 2021
Dim oCC As ContentControl
Dim sName As String
Dim sPath As String
Dim arrInvalid() As String
Dim lng_Index As Long
    'Define illegal filename characters (by ASCII CharNum)
    arrInvalid = Split("9|10|11|13|34|42|47|58|60|62|63|92|124", "|")

    sPath = Environ("USERPROFILE") & "\Desktop\"

    For Each oCC In ActiveDocument.ContentControls
        If oCC.ShowingPlaceholderText = True Then
            oCC.Range.Select
            MsgBox "Complete the field " & oCC.Title, vbExclamation
            Exit Sub
        End If
    Next oCC

    Set oCC = ActiveDocument.SelectContentControlsByTitle("Date").Item(1)
    sName = Format(CDate(oCC.Range.Text), "yyyymmdd")

    Set oCC = ActiveDocument.SelectContentControlsByTitle("Time").Item(1)
    sName = sName & Format(CDate(oCC.Range.Text), "_hhmm")

    Set oCC = ActiveDocument.SelectContentControlsByTitle("Number").Item(1)
    sName = sName & oCC.Range.Text

    For lng_Index = 0 To UBound(arrInvalid)
        sName = Replace(sName, Chr(arrInvalid(lng_Index)), Chr(95))
    Next lng_Index

    MsgBox sName

    ActiveDocument.SaveAs2 sPath & sName & ".docx"
    Set oCC = Nothing
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