![]() |
|
#6
|
||||
|
||||
|
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 |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Finding specific text in IF-fields across multiple Word documents
|
Office_Worker | Word VBA | 7 | 10-19-2017 03:50 AM |
Merging multiple fields within a Word letter from Excel
|
LindsayV | Mail Merge | 2 | 04-08-2016 07:17 AM |
| Mac Word save to PDF creates multiple documents | cmbhome | Word | 0 | 02-18-2015 07:15 PM |
Updating fields in word on save
|
Macer | Word VBA | 8 | 02-09-2015 03:31 PM |
| Word 2003 form fields and pasting multiple paragraphs. | mwmoron | Word | 1 | 12-07-2012 11:06 PM |