1. 程式人生 > >Word 2007 轉 Word2003格式 docx2doc

Word 2007 轉 Word2003格式 docx2doc

init als rest try || multi eve str off

啥也不說了,直接上代碼。

    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            var dilog = new OpenFileDialog();
            dilog.RestoreDirectory = true;
            dilog.AddExtension = true;
            dilog.CheckFileExists = true;
            dilog.DefaultExt = "*.docx";
            dilog.Multiselect = true;
            dilog.Filter = "Word 2010 (*.docx))|*.docx";
            if (dilog.ShowDialog() == DialogResult.OK || dilog.ShowDialog() == DialogResult.Yes)
            {
                var files = dilog.FileNames;
                if (files == null || files.Length < 1)
                {
                    MessageBox.Show("未找到docx,請再重新選擇文件夾");
                    return;
                }
                Log($"將開始轉換{files.Length}個文件");

                var wordApp = new Microsoft.Office.Interop.Word.Application();

                foreach (var item in files)
                {
                    if (!File.Exists(item) || !item.EndsWith(".docx"))
                    {
                        Log($"文件{item}不適合轉換");
                        continue;
                    }

                    var src = item;
                    var dst = src.Substring(0, src.Length - 1);

                    Log($"開始轉換{src}");
                    try
                    {
                        Docx2Doc(wordApp, src, dst);
                        Log($"轉換成功{dst}");
                    }
                    catch (Exception ex)
                    {
                        Log($"轉換失敗{ex}");
                    }
                }

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

        void Log(string msg)
        {
            txtLog.Text = $"{msg}\r\n{txtLog.Text}";
            if(txtLog.Text.Length > 5000)
            {
                txtLog.Text = txtLog.Text.Substring(0, 5000);
            }
        }


        static void Docx2Doc(Microsoft.Office.Interop.Word.Application wordApp, object sourceFileName, object targetFileName)
        {
            object missingValue = System.Reflection.Missing.Value;

            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 = 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);

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

Word 2007 轉 Word2003格式 docx2doc