如何在C#.NET编程中使用MS Office 2010库将.dot文件转换为.docm [英] How to convert .dot file to .docm using MS office 2010 library in C#.NET programming

查看:108
本文介绍了如何在C#.NET编程中使用MS Office 2010库将.dot文件转换为.docm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我必须在C#.NET中编写代码,该代码使用MS Office 2010库并将输入作为.dot文件并将其转换为.dotm或.docm。请帮帮我。

解决方案

.dot是一个模板文件。您不会将模板转换为文档。您使用模板创建新文档,模板不会更改。



请参阅文档的添加方法,https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.documents .add.aspx [ ^ ]。请注意如何传递用于新文档的模板名称。


如果您能够创建基于模板的文档,那么您现在需要做的就是使用< a href =https://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.saveas.aspx> Document.SaveAs方法 [ ^ ]正确< a href =https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.wdsaveformat.aspx> FileFormat(WdSaveFormat) [ ^ ]作为方法的参数。


得到了解决方案:



string docFileName = @C:\ MyFolder \ Test.dot;

string dotmFileName = @C:\ MyFolFolder \Test.dotm;

_Application myWordApp = new Application();

_Document myDoc = myWordApp.Documents.Open(FileName:docFileName,ReadOnly:true);





myDoc.SaveAs2(FileName:dotmFileName,FileFormat:WdSaveFormat.wdFormatXMLTemplateMacroEnabled);

myDoc.Close();

myWordApp.Quit();

Hi,

I have to write a code in C#.NET which uses MS Office 2010 library and takes input as .dot file and convert it to .dotm or .docm. Please help me out.

解决方案

A .dot is a template file. You do not convert templates to documents. You use a template to create a new document and the template does not change.

See the Add method of Documents, https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.documents.add.aspx[^]. Notice how you pass in the name of the template to use for your new document.


If you're able to create document nased on template, all you need to do now is to use Document.SaveAs method[^] with proper FileFormat (WdSaveFormat)[^] as an argument of method.


Got the solution:

string docFileName = @"C:\MyFolder\Test.dot";
string dotmFileName = @"C:\MyFolder\Test.dotm";
_Application myWordApp = new Application();
_Document myDoc = myWordApp.Documents.Open(FileName: docFileName, ReadOnly: true);


myDoc.SaveAs2(FileName: dotmFileName, FileFormat:WdSaveFormat.wdFormatXMLTemplateMacroEnabled);
myDoc.Close();
myWordApp.Quit();


这篇关于如何在C#.NET编程中使用MS Office 2010库将.dot文件转换为.docm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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