public static void CompareInWord(string fullpath, string newFullpath, string saveName, string saveDir, string author, bool save = false) { Object missing = Type.Missing; try { var wordapp = new Microsoft.Office.Interop.Word.Application(); try { var doc = wordapp.Documents.Open(fullpath, ReadOnly: true); doc.Compare(newFullpath, author ?? missing); doc.Close(WdSaveOptions.wdDoNotSaveChanges); // Close the original document var dialog = wordapp.Dialogs[WdWordDialog.wdDialogFileSummaryInfo]; // Pre-set the save destination by setting the Title in the save dialog. // This must be done through reflection, since "dynamic" is only supported in .NET 4 dialog.GetType().InvokeMember("Title", BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty, null, dialog, new object[] {saveName}); dialog.Execute(); wordapp.ChangeFileOpenDirectory(saveDir); if (!save) { wordapp.ActiveDocument.Saved = true; } wordapp.Visible = true; wordapp.Activate(); // Simple hack to bring the window to the front. wordapp.ActiveWindow.WindowState = WdWindowState.wdWindowStateMinimize; wordapp.ActiveWindow.WindowState = WdWindowState.wdWindowStateMaximize; } catch (Exception ex) { Logger.LogException(ex); ShowMessageBox("Word could not open these documents. Please edit the file manually.", "Error"); wordapp.Quit(); } } catch (Exception ex) { Logger.LogException(ex); ShowMessageBox("Could not start Microsoft Word. Office 2003 or higher is required.", "Could not start Word"); } }
public override DocumentEntity ConvertToHtml(DocumentEntity _docEntity) { if (!_docEntity.isConvert && String.IsNullOrEmpty(_docEntity.ConvertError)) { //var imagePath = Path.Combine(_docEntity.ResourcesPath, _docEntity.ImageFolder); Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document doc = word.Documents.Open(_docEntity.FilePath); try { //记录解析前的页面数 //_docEntity.HtmlData.PageNumber = doc.PageSetup.PaperSize var outputpath = Path.Combine(_docEntity.ResourcesPath, "Convert"); _docEntity.ImageFolder = "Convert" + @"/" + "output.files"; if (!Directory.Exists(outputpath)) { Directory.CreateDirectory(outputpath); } word.ChangeFileOpenDirectory(outputpath); doc.SaveAs2("output.htm", WdSaveFormat.wdFormatFilteredHTML); _docEntity.HtmlData.HtmlContent.Add(FileUtils.ReadFile(Path.Combine(outputpath, "output.htm"))); _docEntity.isConvert = true; } catch (Exception e) { _docEntity.isConvert = false; _docEntity.ConvertError = e.Message; } doc.Close(Type.Missing, Type.Missing, Type.Missing); word.Quit(Type.Missing, Type.Missing, Type.Missing); } return(_docEntity); }
private void CreateWord() { lock (this) { if (msword == null) { msword = new Word.Application(); msword.Visible = true; msword.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone; msword.ChangeFileOpenDirectory(Directory.GetCurrentDirectory()); msword.AutomationSecurity = MsoAutomationSecurity.msoAutomationSecurityForceDisable; } } }
private void CreateWord() { lock(this) { if (msword == null) { msword = new Word.Application(); msword.Visible = true; msword.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone; msword.ChangeFileOpenDirectory(Directory.GetCurrentDirectory()); msword.AutomationSecurity = MsoAutomationSecurity.msoAutomationSecurityForceDisable; } } }