how to change a word documents filename from visual studio
Is there a way that i can change the name that a word doc will be saved as after i have opened it? What i'm trying to do is open a word doc that i'm using as a template, and then populate the formfields, and then display it for the user, and let them make changes or add things, and then save it where they want. The problem is if the user hits the save button, instead of save as (which i know they will), it saves it over my template. Can anyone think of a way to change the name that the file will be saved as before i display it to the user?
thanks,
smtraber
Answers
Hi Smtraber,
How did you open the template, by codes?
If that is the case, I think you can open your template document as read-only which is specified by a parameter of documents.Open() function. Since the template document is open as read-only, when users try to save it, Word will ask users save it as another document automatically.
See: http://msdn2.microsoft.com/en-us/library/tcyt0y1f(VS.80).aspx
Another alternative way is that we should subscribe the DocumentBeforeSave event of that document. In the event handle, set Cancel paramter to be true, and call SaveAs() method by our own.
Refer: http://msdn2.microsoft.com/en-us/library/aa263712(office.10).aspx
Thanks
Ji
Rather than using the Open method to open the file, use the ADD method. That will create a new document from the file you're using as a "template". The first time the Save command is executed, this will automatically trigger Save As (up to this point, the document won't have a name).
There is no way to give a document file a name while it is open, except by using SaveAs. If for some reason you weren't able to automate Word and therefore couldn't use the Documents.Add method, an alternative would be to first copy the original file to a new location and rename it. Only after that open it in Word (in this case, I assume you'd be using something like StartProcess). But since you're automating Word (filling in form fields, etc) I recommend you use the Add method.
All Replies
Hi Smtraber,
How did you open the template, by codes?
If that is the case, I think you can open your template document as read-only which is specified by a parameter of documents.Open() function. Since the template document is open as read-only, when users try to save it, Word will ask users save it as another document automatically.
See: http://msdn2.microsoft.com/en-us/library/tcyt0y1f(VS.80).aspx
Another alternative way is that we should subscribe the DocumentBeforeSave event of that document. In the event handle, set Cancel paramter to be true, and call SaveAs() method by our own.
Refer: http://msdn2.microsoft.com/en-us/library/aa263712(office.10).aspx
Thanks
Ji
Rather than using the Open method to open the file, use the ADD method. That will create a new document from the file you're using as a "template". The first time the Save command is executed, this will automatically trigger Save As (up to this point, the document won't have a name).
There is no way to give a document file a name while it is open, except by using SaveAs. If for some reason you weren't able to automate Word and therefore couldn't use the Documents.Add method, an alternative would be to first copy the original file to a new location and rename it. Only after that open it in Word (in this case, I assume you'd be using something like StartProcess). But since you're automating Word (filling in form fields, etc) I recommend you use the Add method.
- It is true,
BUT
When using Add method with document as parameter - word will name the new document after the original file name
When using Add method with template as parameter - word will NOT name the new document after the original file name
I wonder why - Asaf.Meir said:
It is true,
BUT
When using Add method with document as parameter - word will name the new document after the original file name
When using Add method with template as parameter - word will NOT name the new document after the original file name
I wonder why
Are you sure you used the Add method, and not the Open method? I just did a quick test and the name of the new document follows Word's standard naming pattern for new documents: Document2
Cindy Meister, VSTO/Word MVP

