View Single Post
 
Old 09-20-2022, 10:45 AM
Zach Jennings Zach Jennings is offline Windows 10 Office 2021
Novice
 
Join Date: Sep 2022
Posts: 1
Zach Jennings is on a distinguished road
Question Name a new document according to a string it contains

Quote:
Originally Posted by macropod View Post
You could add code like the following to each template's 'ThisDocument' module. As coded, it prompts the user to save the file immediately it's created and defaults to whatever path is specified for the StrTmpPath variable and with whatever filename's specified for the StrName variable (which i've coded for the user's name and the system date in ISO format.
Code:
Private Sub Document_New()
Dim StrName As String, StrDefPath As String, StrTmpPath As String
StrName = Environ("UserName") & Format(Now, "YYYYMMDD")
StrTmpPath = "Filepath for documents based on this template"
StrDefPath = Options.DefaultFilePath(wdDocumentsPath)
Options.DefaultFilePath(wdDocumentsPath) = StrTmpPath
With Application.Dialogs(wdDialogFileSaveAs)
  .Name = StrName
  .Show
End With
Options.DefaultFilePath(wdDocumentsPath) = StrDefPath
End Sub
Hey Paul,

This code works wonders form me great job. I have one request though. Is it at all possible to change the code at the strname variable from the user of the system to a string in the actual document. I'm creating a form for my employees to enter clients information and have created a field for the customer name that I want the code to use as the name of the file. If it helps at all I have mapped the customer name to an xml node that i use for filling other fields throughout the document.

Thanks,
Zach Jennings

Last edited by macropod; 09-20-2022 at 02:42 PM. Reason: Split from: https://www.msofficeforums.com/word/11748-add-save-button-form-template.html
Reply With Quote