码迷,mamicode.com
首页 > 其他好文 > 详细

Word 2007 转 Word2003格式 docx2doc

时间:2017-08-09 23:47:57      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:native   nbsp   data   开始   saveas   适合   comm   extension   ado   

啥也不说了,直接上代码。

    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

标签:native   nbsp   data   开始   saveas   适合   comm   extension   ado   

原文地址:http://www.cnblogs.com/tryc/p/7329299.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!