2015-10-16 87 views
0

我在我的项目中嵌入Word模板文件,我增加一条,作为资源(Resources.resx - >添加资源 - >添加现有的文件),现在我想打开它喜欢的事,这如何打开嵌入式资源word文档?

Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application(); 
Document document = application.Documents.Open(Properties.Resources.MyDoc); 

但不幸的是,Microsoft.Office.Interop.Word.Application不能用于字节数组,我无法将MyDoc设置为它。

回答

2

Word只能打开存在于文件系统中的文件,它不能完全从内存中运行。

做这样的事情:

String fileName = Path.GetTempFileName(); 
File.WriteAllBytes(fileName , Properties.Resources.MyDoc); 
application.Documents.Open(fileName ); 

然后当你检测到的词已经被关闭,删除文件:

File.Delete(fileName); 

这可能是一个想法(因为性能原因)嵌入Word文档作为嵌入式资源而不是resx文件中的Byte[]阵列,如下所示:

Assembly thisExe = System.Reflection.Assembly.GetExecutingAssembly(); 
System.IO.Stream resourceStream = thisExe.GetManifestResourceStream("MyDoc.docx"); 
// copy the stream to a new FileStream, then open Word as-before