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

陌路散爱 提交于 2019-12-01 18:45:22
Fionnuala

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.

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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!