1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
private void ConvertDocToDocx(string docFilePath, string outputDocxFilePath)
{
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
app.Visible = false;
var doc = OpenDocument(app, docFilePath, false);
SaveDocAsDocx(app, doc, outputDocxFilePath);
app.Quit(ref ObjectConstants.False, ref ObjectConstants.MissingValue,
ref ObjectConstants.MissingValue);
}
private void SaveDocAsDocx(Microsoft.Office.Interop.Word.Application app, Microsoft.Office.Interop.Word.Document doc,
object outputFilePath)
{
object format = WdSaveFormat.wdFormatXMLDocument;
doc.SaveAs(ref outputFilePath, ref format,
ref ObjectConstants.False, ref ObjectConstants.MissingValue,
ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue,
ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue,
ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue,
ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue,
ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue,
ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue);
}
private Microsoft.Office.Interop.Word.Document OpenDocument(Microsoft.Office.Interop.Word.Application app, object filePath, bool visible)
{
var doc = (Microsoft.Office.Interop.Word.Document)app.Documents.Open(ref filePath, ref ObjectConstants.MissingValue,
ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue,
ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue,
ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue,
ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue,
ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue,
ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue,
ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue);
if (!visible)
{
doc.ActiveWindow.Visible = false;
}
return doc;
}
public class ObjectConstants
{
public static Object MissingValue = Missing.Value;
public static Object True = true;
public static Object False = true;
} |
Partager