Programmatically Open Word Document Located in the Resource1.resx in C#

0

Im using WinForms and i try to open MS Word document (with some help info) on button click from my form

My code :

using Microsoft.Office.Interop.Word;

Microsoft.Office.Interop.Word.Application ap = new Microsoft.Office.Interop.Word.Application();
Document document = ap.Documents.Open(Resource1.sign_full);

My .docx file is sign_full.docx. I added it to my project Resource1.resx file. Then If I press button1 i get

Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH)) error in "ap.Documents.Open(Resource1.sign_full)" line

Is there any way to open it using Documents.Open? Or shoud i use somthing else?

c#
ms-word

1 Answer

2

The answer could be found here: How to open embedded resource word document?

Word can only open files that exist in the filesystem, it cannot work entirely from-memory. Do something like this:

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

Then when you've detected Word has been closed, delete the file:

File.Delete( fileName );
answered on Stack Overflow Dec 20, 2018 by Ahmad

User contributions licensed under CC BY-SA 3.0