Site icon Ryadel

How to open and edit a Word DOC or DOCX file from FileSystem or Byte array in C#

Convertire file Word DOC e DOCX in sintassi Wiki MediaWiki con un Add-In ufficiale Microsoft

If you're working with ASP.NET C# and you need to open, edit or otherwise access a Microsoft Word DOC or DOCX file, you can easily do that using the Microsoft.Office.Interop.Word library package. This post explains how to do so: you might find it useful in case you need to perform such task or whenever you want to read some insights regarding the process.

Introducing Interop.Word

To access the namespace from your ASP.NET project you have two main choices:

Needless to say, you should really go for the second option, but we'll leave that to you.

Working with the Document

As soon as you have the namespace available, you can do the following:

Once you have the app and the doc objects you can perform a lot of editing task, such as:

Find and Replace Text

Find and replace Bookmarks

Convert a DOC / DOCX file to PDF

Surprisingly enough, we can even do that with an one-liner thanks to the native "Save As PDF..." feature introduced with Office 2010.

Export a DOC / DOCX file into a PDF

This one is almost identical to the previous one in terms of results.

... and so on.

For additional info regarding word-to-pdf conversion, you can also read this dedicated post: otherwise, keep reading.

From a Byte Array

What if you have the DOC or DOCX file stored outside the FileSystem, such as in blob-format within a Database? If that's the case you need to use a temporary file, because most Office Interop methods do not support working with byte arrays, streams and so on.

Here's a decent workaround you can use:

You might notice that we used the

Close()

method in order to close (and thus save) the file. In case you wan't to save your changes to the DOC / DOCX file you opened, you need to explicitly say it by adding the

WdSaveOptions.wdDoNotSaveChanges

object parameter in the following way:

IMPORTANT: Do not underestimate the call to

app.Quit()

! If you don't do that, the MS Word instance will be left open on your server (see this thread on StackOverflow for more info on that issue). If you want to be sure to avoid such dreadful scenario entirely you should strengthen the given implementation adding a try/catch fallback strategy such as the follow:

Unfortunately these objects don't implement IDisposable, otherwise it would've been even easier.

That's pretty much it: happy coding!

 

Exit mobile version