Word 2007 转 Word2003格式 docx2doc
Posted Tryc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Word 2007 转 Word2003格式 docx2doc相关的知识,希望对你有一定的参考价值。
啥也不说了,直接上代码。
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的主要内容,如果未能解决你的问题,请参考以下文章
Office VSTO Word 2003 项目不断尝试自动转换为 2007