Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-29-2021, 12:28 AM
czbbflier czbbflier is offline Save Word doc using multiple fields in the doc Windows 10 Save Word doc using multiple fields in the doc Office 2016
Novice
Save Word doc using multiple fields in the doc
 
Join Date: Jan 2021
Posts: 3
czbbflier is on a distinguished road
Question 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!
Reply With Quote
  #2  
Old 01-29-2021, 05:28 AM
gmayor's Avatar
gmayor gmayor is offline Save Word doc using multiple fields in the doc Windows 10 Save Word doc using multiple fields in the doc Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
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 of
Default

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
Reply With Quote
  #3  
Old 01-29-2021, 12:02 PM
czbbflier czbbflier is offline Save Word doc using multiple fields in the doc Windows 10 Save Word doc using multiple fields in the doc Office 2016
Novice
Save Word doc using multiple fields in the doc
 
Join Date: Jan 2021
Posts: 3
czbbflier is on a distinguished road
Default

Date, time, and Custom fields.
Reply With Quote
  #4  
Old 01-29-2021, 09:27 PM
gmayor's Avatar
gmayor gmayor is offline Save Word doc using multiple fields in the doc Windows 10 Save Word doc using multiple fields in the doc Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
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 of
Default

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
Reply With Quote
  #5  
Old 01-30-2021, 02:47 AM
czbbflier czbbflier is offline Save Word doc using multiple fields in the doc Windows 10 Save Word doc using multiple fields in the doc Office 2016
Novice
Save Word doc using multiple fields in the doc
 
Join Date: Jan 2021
Posts: 3
czbbflier is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
Are they content controls, legacy form fields or activeX fields?
The naming is simple enough but each type of field requires a different approach.
I honestly don’t know.

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?
Reply With Quote
  #6  
Old 01-30-2021, 05:22 AM
gmayor's Avatar
gmayor gmayor is offline Save Word doc using multiple fields in the doc Windows 10 Save Word doc using multiple fields in the doc Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
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 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
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Save Word doc using multiple fields in the doc Finding specific text in IF-fields across multiple Word documents Office_Worker Word VBA 7 10-19-2017 03:50 AM
Save Word doc using multiple fields in the doc 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
Save Word doc using multiple fields in the doc 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

Other Forums: Access Forums

All times are GMT -7. The time now is 10:50 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft