How to set the file name of a word document without saving it from c# and automation
Asked Answered
P

2

6

I create word documents via automation and mailmerge with office Interop from c# (.net4). The merge works well and the user receives the created document as he desires. So far all is well. The client now requests, that if he tries to save the generated file (via save file dialog), that the filename of the document is already prefilled.

I already have tried the obvious things such as setting the Name property of the Document-instance and other properties, I also googled for a solution but up to now, I was not able to set the file-name of the word file (without saving it).

Does someone knows a way how to achieve this? I feel that the client would be very happy if it would work, and I also spent already a lot of time on this (I have to admit that word automation is a thing I have not a lot of experience).

Picnic answered 23/11, 2012 at 15:36 Comment(7)
File.Move(oldName, newName)?Juetta
@Oded: Do you mean System.IO.File.Move? If yes: The file does not yet exist, it's the result of a mail merge and opened unsaved. Or do I missunderstand your proposition?Picnic
No, you understood correctly. Wasn't sure that the file was not in existence yet.Juetta
Did you look at Document.SaveAs?Juetta
@Oded: Save the file to a temporary location and defining with this the filename? I will try, however I fear that then the Save-Operation does no more show the save-file dialog (on hitting save or pressing ctrl-s) and this is not the desired behaviour. The prefilled filename only should be a proposition to the user. Or do you know an option which prevents this behaviour? If yes, this would be a solution!Picnic
I think Word defaults the file name to the first line in the document. Could you add a first line that is the default file name?Bermudez
@Oded: SaveAs has the negative side effect I have feared. Sadly therefore this seems not to be a possible solution. But thanks anyway!Picnic
N
7

If you set the Title property of the document, when you choose Save As, that is the document name that will be used. You can also set the default save location. In VBA

Set doc = ActiveDocument

sTitle = doc.BuiltInDocumentProperties("Title").Value
doc.BuiltInDocumentProperties("Title").Value = "A different title"

However, this only works on the second (and later) save attempt(s). The first attempt will always use the title from the template, if any, or content from the first line of the document if none. See the end of this answer for a better solution.

Note, however, that you must make some change to the document before Save As for the new title to take effect.

Sub SetSummaryInfo()
Dim dp As Object
Dim sTitle As String
    If Documents.Count > 0 Then
       Set dp = Dialogs(wdDialogFileSummaryInfo)
       ' Retrieve value of "Title" into a variable.
       sTitle = dp.Title
       ' Set "Title" to a new value.
       dp.Title = "My Title"
       ' Set the value without showing the dialog.
       dp.Execute
       ' Save the changes
       'ActiveDocument.Save
    End If
End Sub

As remarked by HCL in C#, you can set the default filename (for the dialog only) using this code:

dynamic dialog = wordApp.Dialogs[WdWordDialog.wdDialogFileSummaryInfo]; 
dialog.Title = "MyTitle"; 
dialog.Execute();

This opens the standard "Save As" dialog, sets the default filename (not what you'd expect from a 'Title' property but that's what it does), and opens the dialog.

Nitrobenzene answered 23/11, 2012 at 16:12 Comment(7)
Selection.TypeText Text:=" " is about the minimum change. Make sure you are at the end of the document.Nitrobenzene
Thanks, but sadly this does not apply the change (Word 2010). But I will try further, maybe I'm making a mistake, I'm quite a noob in automation.Picnic
That is odd, what works is not the change but choosing Save As twice. Odd.Nitrobenzene
Yes, I have also observed this, but up to now, I have not found a possibility to apply the change via automation. Even a call to Undo and then Redo does not apply!Picnic
Okay, I think I have it. The second option above works straight off, even with a blank document.Nitrobenzene
+1 This is great! You have saved me a lot of time searching (and probably I would not have found the solution). Thank you a lot! Here the code in c#, I had to search a while, for me, it's not very obvious. Maybe you integrate it into your answer? dynamic dialog = wordApp.Dialogs[WdWordDialog.wdDialogFileSummaryInfo]; dialog.Title = "MyTitle"; dialog.Execute();Picnic
@Nitrobenzene If I specify heading 256.00.000-P-01, then only 256 appears in the file name field in the Save As window. How can I solve this problem? Or is it a limitation MS Word.Perkoff
C
0

The docs
http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.saveas%28v=vs.80%29.aspx
seems to say that you CAN specify a filename or am I missing something?

Cerebellum answered 23/11, 2012 at 15:57 Comment(2)
Yes, saving with a specific filename is not a problem. The problem is, that the file must not be saved but if the client presses ctrl-s, the save-file dialog should already have prefilled a file Name. But thanks anyway for your answer.Picnic
If you meant the file name of downloaded file than you should take a look at response header "content-disposition". I hope that I understood you correctly this time.Cerebellum

© 2022 - 2024 — McMap. All rights reserved.