View Full Version : New document auto Save as macro
pete smith
11-11-2006, 04:45 AM
I am attempting to create a macro so that when a new document is created from a document template, the user is prompted to save the file. The date and format of file is provided in the prompt for the file name within the dialog box.
The problem:
If the user attempts to give the document the same name as an open document (eg doesn't change the file name) Then a run-time error appears.
Instead of the runtime error appearing what code can I add so that the user is prompted to save the file as a different name. Any suggestions, I'm stumped.
ALSO
Is there any way to bring up the document properties dialogue box rather than the file summary info dialogue box - I want to access the 'categories' field.
Thanks Pete
Code below:
Sub newthensave()
'
'newthensave Macro
Documents.Add Template:="C:\STANDARDS\MS Office\HAB_Letter.dot", _
NewTemplate:=False, DocumentType:=0
Set dlgSaveAs = Dialogs(wdDialogFileSaveAs)
With dlgSaveAs
.Name = "c:\standards\" & "XXX" & Format(Now(), "yymmdd") & "_subject"
.Show
End With
Dialogs(wdDialogFileSummaryInfo).Show
End Sub
fumei
11-11-2006, 06:54 PM
I am attempting to create a macro so that when a new document is created from a document template, the user is prompted to save the file. The date and format of file is provided in the prompt for the file name within the dialog box.
The problem:
If the user attempts to give the document the same name as an open document (eg doesn't change the file name) Then a run-time error appears.
Instead of the runtime error appearing what code can I add so that the user is prompted to save the file as a different name. Any suggestions, I'm stumped.You ARE prompting the user to save with a different name! In fact, you are GIVING them a different name, already in the dialog.
If the user changes the name you are supplying, and uses the name of an open document...shrug...that is their fault/problem.
Repeat, you ARE prompting them to save with a different name. In fact:
user attempts to give the document the same name as an open document (eg doesn't change the file name) is not accurate. If the user attempts to give a name as an open document, they ARE changing the file name. I am assuming the one you put in the dialog is NOT an open document.
If you wanted, you could run through all the open documents and check against their names. But, again, if you give them a name, and they change it...then they change it.
Is there any way to bring up the document properties dialogue box rather than the file summary info dialogue box - I want to access the 'categories' field.It is "Category", not categories. It is one of the BuiltInDocumentProperties.
Not that I know of.
You can read and write to it though. They are accessible.Selection.EndKey unit:=wdStory
Selection.TypeText Text:=ActiveDocument. _
BuiltInDocumentProperties("Category") & vbCrLf
ActiveDocument.BuiltInDocumentProperties("Category") = "Blah Blah"
Selection.TypeText Text:=ActiveDocument. _
BuiltInDocumentProperties("Category")goes to the end of the document, types the existing Category, changes the text of the Category to "Blah Blah", and then types the existing Category (now "Blah Blah").
fumei
11-11-2006, 06:56 PM
I have to add, if there is a procedure to name the new file, why are you even giving the user a dialog that they can change? Why not just save the new file with the name you want?
mdmackillop
11-12-2006, 03:41 AM
You can check for a duplicate name and add an increment if it exists
From my KB item http://vbaexpress.com/kb/getarticle.php?kb_id=290
'Check for previous instances of the SaveName, if found add incremental suffix
With Application.FileSearch
.NewSearch
.LookIn = Direct
.SearchSubFolders = False
.FileName = SaveName & "*"
.FileType = msoFileTypeAllFiles
If .Execute() > 0 Then
SaveAsName = SaveName & "-" & .FoundFiles.Count & ".doc"
End If
End With
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.