#1
|
|||
|
|||
Save Word doc using multiple fields in the doc
Greetings-
I am trying to find a way to create a standard naming convention using the fields in the document. It would involve the date, time, and 5 numeric field entries, with spaces between each of them. I can do this with VBA in Excel, but I can't find anything using VBA in Word. Thanks! |
#2
|
||||
|
||||
What sort of fields?
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Date, time, and Custom fields.
|
#4
|
||||
|
||||
Are they content controls, legacy form fields or activeX fields?
The naming is simple enough but each type of field requires a different approach.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#5
|
|||
|
|||
Quote:
I’m using word for a office 365 (the non-online version). I’m currently using the selections first available under the Developer tab. The last of these icons is a pull-down which gives me a choice of legacy and active-x. The first bunch of icons don’t tell me what they are. Which ones are easier to program with? |
#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 |
Thread Tools | |
Display Modes | |
|
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 |