Saving multiple Word documents as HTML through the Office API

I have a large number of Word documents that I need to parse. Since they were all created from the same template, I think the best approach is to save them as HTML files and parse the HTML itself.

Although it is quite simple to save one Word document as HTML, I have not found a way to make a bulk procedure from Word. So I'm trying to find a way to use the Microsoft Office / Word API to accomplish this.

How can I use the Word API to save many Word documents as HTML?

Thanks in advance.

UPDATE: A few more details ...

Some of the documents have an extension .doc, while others - .docx. I hope this is not a problem, but if so, I just need to convert them all to .docx, hopefully, using an API or DocX .

Speaking of DocX, I saw on the author’s blog that you can save the file .docxas HTML with the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.Word;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Convert Input.docx into Output.doc
            Convert(@"C:\users\cathal\Desktop\Input.docx", @"c:\users\cathal\Desktop\Output.doc", WdSaveFormat.wdFormatDocument);

            /*
             * Convert Input.docx into Output.pdf
             * Please note: You must have the Microsoft Office 2007 Add-in: Microsoft Save as PDF or XPS installed
             * http://www.microsoft.com/downloads/details.aspx?FamilyId=4D951911-3E7E-4AE6-B059-A2E79ED87041&displaylang=en
             */
            Convert(@"c:\users\cathal\Desktop\Input.docx", @"c:\users\cathal\Desktop\Output.pdf", WdSaveFormat.wdFormatPDF);

            // Convert Input.docx into Output.html
            Convert(@"c:\users\cathal\Desktop\Input.docx", @"c:\users\cathal\Desktop\Output.html", WdSaveFormat.wdFormatHTML);
        }

        // Convert a Word 2008 .docx to Word 2003 .doc
        public static void Convert(string input, string output, WdSaveFormat format)
        {
            // Create an instance of Word.exe
            Word._Application oWord = new Word.Application();

            // Make this instance of word invisible (Can still see it in the taskmgr).
            oWord.Visible = false;

            // Interop requires objects.
            object oMissing = System.Reflection.Missing.Value;
            object isVisible = true;
            object readOnly = false;
            object oInput = input;
            object oOutput = output;
            object oFormat = format;

            // Load a document into our instance of word.exe
            Word._Document oDoc = oWord.Documents.Open(ref oInput, ref oMissing, ref readOnly, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref isVisible, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

            // Make this document the active document.
            oDoc.Activate();

            // Save this document in Word 2003 format.
            oDoc.SaveAs(ref oOutput, ref oFormat, 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);

            // Always close Word.exe.
            oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
        }
    }
}

Is this the best way to do this?

+5
source share
1 answer

, , . , , Document.SaveAs Api (docx, doc, rtf), Word HTML ( )

[] api ,

+4

All Articles