Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-13-2014, 02:09 AM
rosscortb rosscortb is offline Save As Macro that changes the file name also Windows XP Save As Macro that changes the file name also Office 2010 32bit
Novice
Save As Macro that changes the file name also
 
Join Date: May 2014
Posts: 17
rosscortb is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 05-13-2014, 09:04 AM
Charles Kenyon Charles Kenyon is offline Save As Macro that changes the file name also Windows 7 64bit Save As Macro that changes the file name also Office 2010 32bit
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,140
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

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
Reply With Quote
  #3  
Old 05-13-2014, 09:48 AM
rosscortb rosscortb is offline Save As Macro that changes the file name also Windows XP Save As Macro that changes the file name also Office 2010 32bit
Novice
Save As Macro that changes the file name also
 
Join Date: May 2014
Posts: 17
rosscortb is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 05-13-2014, 10:51 AM
Charles Kenyon Charles Kenyon is offline Save As Macro that changes the file name also Windows 7 64bit Save As Macro that changes the file name also Office 2010 32bit
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,140
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

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
I believe if instead of .Show it was .Execute, it would simply save without user input unless there was already a file with that name.

You would use the ChangeFileOpenDirectory method to change to the folder you want to use.
Reply With Quote
  #5  
Old 05-19-2014, 06:55 AM
rosscortb rosscortb is offline Save As Macro that changes the file name also Windows XP Save As Macro that changes the file name also Office 2010 32bit
Novice
Save As Macro that changes the file name also
 
Join Date: May 2014
Posts: 17
rosscortb is on a distinguished road
Default

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
Reply With Quote
  #6  
Old 05-19-2014, 08:40 AM
macropod's Avatar
macropod macropod is offline Save As Macro that changes the file name also Windows 7 32bit Save As Macro that changes the file name also Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
Reply



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
Save As Macro that changes the file name also 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
Save As Macro that changes the file name also A newbie question: a must to save macro word file as .docm? tinfanide Word VBA 6 12-06-2011 03:02 PM
Save As Macro that changes the file name also 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

Other Forums: Access Forums

All times are GMT -7. The time now is 12:44 AM.


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