问题
I have a large amount of Word documents that I need to parse. As they all were created from the same template, I think that the best approach would be to save them as HTML files and parse the HTML itself.
While it's quite easy to save a single Word document as HTML, I haven't found a way to do a bulk procedure from inside Word. Thus, I'm trying to find a way to leverage 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 are of extension .doc
, while others are .docx
. I hope that this isn't a problem, but if it is, I'll just have to convert them all to .docx
, hopefully with the API or with DocX.
Speaking of DocX, I saw on the author's blog that it's possible to save a .docx
file as 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 it?
回答1:
The code you have posted above should do the job for you. Also as far as i know Document.SaveAs Api can convert any document(docx,doc,rtf) which it can open in word to HTML(or any other format)
also instead of creating a word application instance for each file pass the string[] of names to the convert api and only dispose document instance once you are done with save as
来源:https://stackoverflow.com/questions/3694090/saving-multiple-word-documents-as-html-through-office-api