Vahid
Vahid

Reputation: 5444

How to embed a word document in .net

I'm trying to embed a word document in my project. This document will be used as a template by a specific library.

I have added the file using Project | Add Existing Item..., I have also set its Build Action to Resource and its Copy to Output Directory to Do Not Copy.

Now in my code behind I'm doing this to access the file:

var template = DocX.Load("pack://application:,,,/doc1.docx");

But I'm getting an exeption: XamlParseException occured.

What am I doing wrong here?

enter image description here

Upvotes: 1

Views: 1883

Answers (1)

Alex
Alex

Reputation: 13224

You need to get the stream to the document from the resources using theAssembly.GetManifestResourceStream method, as in the example below:

var assembly = Assembly.GetExecutingAssembly();
var resourceName = "MyNamespace.doc1.docx"  // check your resource name.
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
    var template = DocX.Load(stream);
}

Also check out this information: How to embed and access resources by using Visual C#

And also change the Build Action to Embedded Resource.

Upvotes: 3

Related Questions