How to open embedded resource word document?

后端 未结 1 631
你的背包
你的背包 2021-01-28 17:10

I have a embedded word template document in my project, I added it as resource (Resources.resx -> Add resource -> Add existing file) and now I want to open it something like thi

相关标签:
1条回答
  • 2021-01-28 17:44

    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.MyDoc );
    application.Documents.Open( fileName  );
    

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

    File.Delete( fileName );
    

    It might be an idea (for performance reasons) to embed the Word document as an Embedded Resource instead of a Byte[] array within a resx file, like so:

    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
    
    0 讨论(0)
提交回复
热议问题