Office之Docx轉Doc操作(c#)

需要引用Microsoft.Office.Interop.Word組件

private void Docx2Doc(object sourceFileName, object targetFileName)
{
    object missingValue = System.Reflection.Missing.Value;

    Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
    Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(
        ref sourceFileName,
        ref missingValue, ref missingValue, ref missingValue, ref missingValue,
        ref missingValue, ref missingValue, ref missingValue, ref missingValue,
        ref missingValue, ref missingValue, ref missingValue, ref missingValue,
        ref missingValue, ref missingValue, ref missingValue);

    object FileFormat = msWord.WdSaveFormat.wdFormatDocument; 
    object LockComments = false;
    object Password = missingValue;
    object AddToRecentFiles = false;
    object WritePassword = missingValue;
    object ReadOnlyRecommended = false;
    object EmbedTrueTypeFonts = true;
    object SaveNativePictureFormat = missingValue;
    object SaveFormsData = missingValue;
    object SaveAsAOCELetter = missingValue;
    object Encoding = missingValue;
    object InsertLineBreaks = missingValue;
    object AllowSubstitutions = missingValue;
    object LineEnding = missingValue;
    object AddBiDiMarks = missingValue;
    object CompatibilityMode = missingValue;

    doc.SaveAs(ref targetFileName, ref FileFormat,
        ref LockComments, ref Password, ref AddToRecentFiles, ref WritePassword,
        ref ReadOnlyRecommended, ref EmbedTrueTypeFonts, ref SaveNativePictureFormat, ref SaveFormsData,
        ref SaveAsAOCELetter, ref Encoding, ref InsertLineBreaks, ref AllowSubstitutions,
        ref LineEnding, ref AddBiDiMarks);

    //doc.SaveAs(ref targetFileName, ref FileFormat, 
    //        ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, 
    //        ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, 
    //        ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, 
    //        ref MissingValue, ref MissingValue);


    wordApp.Documents.Close(ref missingValue, ref missingValue, ref missingValue);

    //關閉進程
    object saveOption = msWord.WdSaveOptions.wdDoNotSaveChanges;
    wordApp.Application.Quit(ref saveOption, ref missingValue, ref missingValue);
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章