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(@"C:\users\cathal\Desktop\Input.docx", @"c:\users\cathal\Desktop\Output.doc", WdSaveFormat.wdFormatDocument);
Convert(@"c:\users\cathal\Desktop\Input.docx", @"c:\users\cathal\Desktop\Output.pdf", WdSaveFormat.wdFormatPDF);
Convert(@"c:\users\cathal\Desktop\Input.docx", @"c:\users\cathal\Desktop\Output.html", WdSaveFormat.wdFormatHTML);
}
public static void Convert(string input, string output, WdSaveFormat format)
{
Word._Application oWord = new Word.Application();
oWord.Visible = false;
object oMissing = System.Reflection.Missing.Value;
object isVisible = true;
object readOnly = false;
object oInput = input;
object oOutput = output;
object oFormat = format;
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);
oDoc.Activate();
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);
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
}
}
}
Is this the best way to do this?