#1
|
|||
|
|||
Save As Macro that changes the file name also
Hello,
I have a template letter document with a userform that insert text at the correct bookmarks. I have recorded a macro that saves the file to a specific folder( easy enough) but I, if possible, I would like to include some code that changes the name of the file when I run the macro. For example, LastName,FirstName Contract Someone has give me this but it doesn't seem to like the ME. If should maybe point out that in the form I am using a txt called txtEmployeeName ( not sure if this is relevant) Any input would be welcomed. Thanks Ross Sub SaveAS() ' ' SaveAS Macro ' Dim NewFileName As String Dim NewFilePath As String NewFilePath = "H:\HR\Reward & Shared Services\Shared Services Only\1 ~ Starters\3 ~ Contract Templates\DB Templates\" NewFileName = Me.txtLastName & "," & Me.txtFirstName & ",Contract.docm" ' ChangeFileOpenDirectory _ "H:\HR\Reward & Shared Services\Shared Services Only\1 ~ Starters\3 ~ Contract Templates\DB Templates\" ActiveDocument.SaveAs2 FileName:= _ "H:\HR\Reward & Shared Services\Shared Services Only\1 ~ Starters\3 ~ Contract Templates\DB Templates\A-D contract v10-MACRO.docm" _ , FileFormat:=wdFormatXMLDocumentMacroEnabled, LockComments:=False, _ Password:="", AddToRecentFiles:=True, WritePassword:="", _ ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _ SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _ False, CompatibilityMode:=14 End Sub |
#2
|
|||
|
|||
Is there some reason you are Opening your template and using SaveAs rather than creating a new document and using Save? The latter is the regular use of a document template.
Templates in Microsoft Word |
#3
|
|||
|
|||
Hi
No reason, would like to save the document as a new document to specific folder but with a new name. Instead of,save as, location folder and type over the template name. Ross |
#4
|
|||
|
|||
OK.
Then your new document is formed when the user double-clicks on the template or uses one of the File New Variations to create a new file. The user does NOT open the template. You can use your UserForm's OK click event to open the File Save As dialog with your proposed name or simply save the document with the name you want. Simply use the variable information you already have to create your filename string Here is code that I use in one of my forms to insert the current date as a part of the name. Code:
Private Sub SaveAsLetter() ' Run as part of first step - saves letters using preferred format for name Dim strName As String, dlgSave As Dialog Set dlgSave = Dialogs(wdDialogFileSaveAs) strName = "Letter " & Format((Year(Now() + 1) Mod 100), "20##") & "-" & _ Format((Month(Now() + 1) Mod 100), "0#") & "-" & _ Format((Day(Now()) Mod 100), "0#") ActiveDocument.BuiltInDocumentProperties("Title").Value = strName With dlgSave .Name = strName .Show End With End Sub You would use the ChangeFileOpenDirectory method to change to the folder you want to use. |
#5
|
|||
|
|||
Hi
Thanks for your help. I should have pointed out that my document wasn't saved as a template, however, I did do this and when it is opens up, it opens as a fresh document, called, Document1 but keeps asking if I wanted to save changes to the template and it stops my user form from opening up automatically. So, as a plain word document, I managed to record a save as macro to specific folder and then used and adapted your code, which allows me rename the file in the save dialog box before hittting save. Do you think this is the best solutions or is a way to do the whole process in one with the document being renamed from grabbing txt from the form or something similar- code below- Sub SaveAsContract() ' ' SaveAsContract Macro ' ' ChangeFileOpenDirectory _ "H:\HR\Reward & Shared Services\Shared Services Only\1 ~ Starters\5 ~ Offers Sent\" ActiveDocument.SaveAs2 FileName:= _ "H:\HR\Reward & Shared Services\Shared Services Only\1 ~ Starters\5 ~ Offers Sent\A-D contract v10-MACRO.docm" _ , FileFormat:=wdFormatXMLDocumentMacroEnabled, LockComments:=False, _ Password:="", AddToRecentFiles:=True, WritePassword:="", _ ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _ SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _ False, CompatibilityMode:=14 Dim strName As String, dlgSave As Dialog Set dlgSave = Dialogs(wdDialogFileSaveAs) strName = "LastName,FirstName Contract " ActiveDocument.BuiltInDocumentProperties("Title"). Value = strName With dlgSave .Name = strName .Show End With End Sub Cheers Ross |
#6
|
||||
|
||||
Cross-posted at: http://www.vbaexpress.com/forum/show...file-name-also
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How do I save a Word file with a macro for distribution? | leemoreau | Word VBA | 3 | 10-04-2013 08:06 AM |
Macro to create new word doc and save the file using String found in the document | VBNation | Word VBA | 2 | 02-08-2013 07:14 AM |
A newbie question: a must to save macro word file as .docm? | tinfanide | Word VBA | 6 | 12-06-2011 03:02 PM |
Word Macro: Save file as text with current file name | jabberwocky12 | Word VBA | 2 | 10-22-2010 12:23 PM |
Macro will not save to normal.dot file when exiting | bobbraun | Word | 1 | 09-28-2010 06:26 AM |