6

I have a folder of word documents that I want to convert to html for further processing. I only have Visual Studio 2010 Express edition. Is it possible using the express edition? I've found examples for how to do the conversion, but they require the Microsoft.Office.Tools.Word library, which doesn't come with Express.

EDIT: I found it, it's actually in the COM object called Microsoft Word 12.0 Object Library, which is the Microsoft.Office.Interop.Word namespace.

Geo
  • 292
  • 1
  • 4
  • 12
  • it comes with office. I guess you can reference it with VS2010 express – Ivo Mar 28 '12 at 03:45
  • Express edition is only an IDE limitation, not a CLR limitation – Seph Mar 28 '12 at 06:02
  • Note, the version of Microsoft Word Object Library will defer based on what version of Microsoft Office you have installed. Also as an FYI, there are solutions that don't require having Office installed. For instance, take a look at this example for [converting between Word and HTML](https://www.gemboxsoftware.com/document/examples/c-sharp-convert-word-to-from-html/105). – Mario Z Mar 31 '20 at 05:43

1 Answers1

10

You should be able to do it with the express version. I adapted an answer to this question. The adapted code is below. You'll need to add a reference to Microsoft.Office.Interop.Word for this to work. If you're missing this library, have a look at this article on MSDN.

Looking at WdSaveFormat you can also save it as Format Filtered HTML (wdFormatFilteredHTML).

namespace Sample {
    using Microsoft.Office.Interop.Word;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;

    class Program {

        public static void Main()
        {
            Convert("C:\\Documents", WdSaveFormat.wdFormatHTML);
        }

        private static void Convert(string path, WdSaveFormat format)
        {

            DirectoryInfo dirInfo = new DirectoryInfo(path);
            FileInfo[] wordFiles = dirInfo.GetFiles("*.doc");
            if (wordFiles.Length == 0) {
                return;
            }

            object oMissing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
            try {
                word.Visible = false;
                word.ScreenUpdating = false;
                foreach (FileInfo wordFile in wordFiles) {
                    Object filename = (Object)wordFile.FullName;
                    Document doc = word.Documents.Open(ref filename, ref oMissing,
                                                       ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                                       ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                                       ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                    try {
                        doc.Activate();
                        object outputFileName = wordFile.FullName.Replace(".doc", ".html");
                        object fileFormat = format;
                        doc.SaveAs(ref outputFileName,
                                   ref fileFormat, ref oMissing, ref oMissing,
                                   ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                   ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                   ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                    }
                    finally {
                        object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
                        ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
                        doc = null;
                    }
                }

            }
            finally {
                ((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
                word = null;
            }
        }
    }
}
Community
  • 1
  • 1
bloudraak
  • 5,902
  • 5
  • 37
  • 52