K
K
KillAstronauts2015-02-05 01:16:25
Programming
KillAstronauts, 2015-02-05 01:16:25

How to open a Word template located in a project resource (*.resx)?

Goodnight!
I'm making a program that opens a Word template (*.dotx) and inserts values ​​into it. In order not to carry the Word template with me, I decided to add it to the project resources (* .resx). But how to open it?
Below is a piece of code for opening a Word template ( peeped here ), in which "templatePathObj" is the path to the template. What to do if the template is in the project resource (*.resx)?

using Word = Microsoft.Office.Interop.Word;
using System.Reflection;

Object missingObj = System.Reflection.Missing.Value;
Object trueObj = true;
Object falseObj = false;

Word.Application application = new Word.Application();
Word.Document document = application.Documents.Add(ref  templatePathObj, ref missingObj, ref missingObj, ref missingObj)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
mayorovp, 2015-02-05
@KillAstronauts

Look for alternative template generating libraries that don't require the template to exist as a separate file on disk.
For example, Open XML SDK

S
Sumor, 2015-02-05
@Sumor

The template from the resources must be saved somewhere on disk, for example, in a temporary folder, and the temporary path must be specified.

B
bmforce, 2015-02-05
@bmforce

If you use the Open XML SDK instead of the COM library to work with Word, you can use the Open method that takes a stream as an argument:
https://msdn.microsoft.com/en-us/library/office/cc...
Otherwise , as already mentioned, only saving the template to a temporary folder will help.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question