public static void Merge(string[] filesToMerge, string outputFilename, bool insertPageBreaks) { Word._Application wordApplication = new Word.Application(); object pageBreak = Word.WdBreakType.wdSectionBreakContinuous; object outputFile = outputFilename; object fileName = @"S:\ODS\Insight 2 Oncology\Quality Division Project\Visual Review Scores\Scoring Template\MergeTemplate.docx"; object end = Word.WdCollapseDirection.wdCollapseEnd; try { wordApplication.Visible = false; Word.Document wordDocument = wordApplication.Documents.Add(ref fileName); Word.Selection selection = wordApplication.Selection; foreach (string file in filesToMerge) { if (file.Contains("Scores.docx")) { selection.PageSetup.PaperSize = Word.WdPaperSize.wdPaper11x17; selection.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape; selection.InsertFile(file); selection.Collapse(ref end); } if (!file.Contains("Scores.docx") && insertPageBreaks) { selection.InsertFile(file); selection.InsertBreak(pageBreak); } } wordApplication.Selection.Document.Content.Select(); wordDocument.PageSetup.TopMargin = (float)50; wordDocument.PageSetup.RightMargin = (float)50; wordDocument.PageSetup.LeftMargin = (float)50; wordDocument.PageSetup.BottomMargin = (float)50; selection.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceSingle; selection.ParagraphFormat.SpaceAfter = 0.0F; selection.ParagraphFormat.SpaceBefore = 0.0F; wordDocument.SaveAs(outputFile + ".docx"); wordDocument.Close(); wordDocument = wordApplication.Documents.Open(outputFile + ".docx"); wordDocument.ExportAsFixedFormat(outputFile + ".pdf", Word.WdExportFormat.wdExportFormatPDF); wordDocument = null; } catch (Exception ex) { throw ex; } finally { wordApplication.Quit(); } }
public static void CombineWord(string fileToMerge1, string fileToMerge2, string outputFilename) { object missing = System.Type.Missing; object pageBreak = Word.WdBreakType.wdPageBreak; object outputFile = outputFilename; // Create a new Word application Word._Application wordApplication = new Word.Application(); try { Word._Document wordDocument = wordApplication.Documents.Open( fileToMerge1 , ref missing , ref missing , ref missing , ref missing); Word.Selection selection = wordApplication.Selection; wordDocument.Merge(fileToMerge2, ref missing, ref missing, ref missing, ref missing); // Save the document to it's output file. wordDocument.SaveAs( ref outputFile , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing); wordDocument = null; } catch (Exception ex) { //I didn't include a default error handler so i'm just throwing the error throw ex; } finally { wordApplication.Quit(ref missing, ref missing, ref missing); } }
private void Application_WindowBeforeRightClickEvent(Word.Selection curselection, ref bool cancel) { GlobalClass.myselection = curselection; var wordrange = GlobalClass.myword.Selection.Range; int left, top, width, height; GlobalClass.myword.ActiveWindow.GetPoint(out left, out top, out width, out height, wordrange); var curword = curselection.Range.Words[1].Text; var curwordcount = curselection.Range.Words.Count; PunchPattern punckPattern = null; if (_listpukPattern != null) { punckPattern = _listpukPattern.Find(t => t.IndexStart <= curselection.Range.Start && t.IndexEnd >= curselection.Range.End); } if (punckPattern != null && punckPattern.ErrorCode > 0) { cancel = true; _currentPuncPattern = punckPattern; _cpuncForm = new CPPPunctuation(); _cpuncForm.Deactivate += CpPuncForm_Deactivate; _cpuncForm.ShowBallonScreen(new Point(left, top)); _cpuncForm.ShowFormWithMessage = punckPattern.ErroMessage + Environment.NewLine + punckPattern.ErrorCorrection; } else { // else spell var isSpellisCorrect = _chkSpell.Cheak_Spell(curword.Trim()); if (curword.Trim().Length > 1 && curwordcount == 1 && isSpellisCorrect == false) { cancel = true; _cspellForm = new CPPSPell(); _cspellForm.Deactivate += cpSpellForm_Deactivate; _cspellForm.ShowBallonScreen(new Point(left, top)); var suggestList = _chkSpell.Suggest(curword); _cspellForm.ShowDlg(suggestList); } } }
/// <summary> /// 删除word文档中相应的内容 /// </summary> /// <param name="startText">起始字符</param> /// <param name="endText">终止字符</param> /// <param name="path">文档路径</param> /// <returns></returns> public bool DeleteText(string startText, string endText, string path) { object OBJpath = path; object missing = System.Reflection.Missing.Value; Word._Application wapp = new Word.ApplicationClass(); Word.Document wdoc = new Word.Document(); wdoc = wapp.Documents.Open(ref OBJpath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); Word.Selection tihuanwenzi = wapp.Selection; wapp.Options.ReplaceSelection = true; wapp.Selection.Find.Forward = true; wapp.Selection.Find.MatchWholeWord = true; object startstr = startText; object endstr = endText; wapp.Selection.Find.ClearFormatting(); wapp.Selection.Find.Execute(ref startstr, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); object start = wapp.Selection.Range.Start;//定位起始位置 wapp.Selection.Find.ClearFormatting(); wapp.Selection.Find.Execute(ref endstr, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); object end = wapp.Selection.Range.End;//定义终止位置 object unit = Type.Missing; object count = Type.Missing; wdoc.Range(ref start, ref end).Delete(ref unit, ref count);//删除操作 object SaveChangs = true; object OriginalFormat = System.Type.Missing; object RouteDocument = System.Type.Missing; wapp.Documents.Close(ref missing, ref missing, ref missing); wapp.Quit(ref SaveChangs, ref OriginalFormat, ref RouteDocument); return(true); }