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!

 

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

5 Comments on “How to open and edit a Word DOC or DOCX file from FileSystem or Byte array in C#”

  1. Pingback: Programmatically convert MS Word files to PDF in ASP.NET C#
  2. Pingback: 80070005 Access is denied error while accessing Word Interop from the Server in a ASP.NET Application - How to fix
  3. Pingback: Come convertire file MS Word DOC e DOCX in PDF con ASP.NET e C#
  4. Pingback: ASP.NET - Generate MS Excel files programmatically with EPPlus

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.