如何设置一个Word文档的文件名,不从C#和自动化其保存 [英] How to set the file name of a word document without saving it from c# and automation

查看:309
本文介绍了如何设置一个Word文档的文件名,不从C#和自动化其保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建通过自动化和邮件合并办公互操作word文件从C#(.NET4)。合并运作良好,用户会收到所创建的文件,因为他的欲望。到目前为止,一切都很好。 现在,客户端请求,如果他试图保存生成的文件(通过保存文件对话框),该文件的文件名已经是prefilled。

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.

我已经尝试了明显的东西,如设置文件实例和其他属性的名称属性,我也用Google搜索的一个解决方案,但到现在为止,我是不是能够设置Word文件的文件名(不保存它)。

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).

推荐答案

如果您设置文档的标题属性,当你选择另存为,这是将要使用的文件名。您还可以设置默认的保存位置。在VBA

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"

但是请注意,你必须做出一些改变文档之前保存为新标题生效。

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

由于在C#中指出由HCL这将是

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

这篇关于如何设置一个Word文档的文件名,不从C#和自动化其保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆