/// <summary> /// 复制第二个文件内容到第一个文件 /// </summary> /// <returns></returns> public string CopyOtherFileContentToWord(string firstFilePath, string secondFilePath, string bookmark, bool isCloseTheFile = true) { try { Document htmldoc = OpenWord(firstFilePath); Document secondFile = OpenWord(secondFilePath); Range range = GetBookmarkRank(secondFile, bookmark); htmldoc.Content.Copy(); range.Select(); range.PasteAndFormat(WdRecoveryType.wdPasteDefault); range.Select(); int tableCount = _wordApp.Selection.Tables.Count; if (tableCount > 0) { foreach (Table table in _wordApp.Selection.Tables) { table.AutoFitBehavior(WdAutoFitBehavior.wdAutoFitWindow); } } if (isCloseTheFile) { CloseWord(htmldoc, firstFilePath); } } catch (Exception ex) { _needWrite = false; Dispose(); throw new Exception(string.Format("错误信息:{0}.{1}", ex.StackTrace.ToString(), ex.Message)); } return("保存成功"); }
public void addSequanceDiagram(ref Word.Document doc) { try { Visio.Document sequenceVisio; string sequenceDiagramPath = Directory.GetCurrentDirectory() + "\\Templates\\" + Constants.ServiceFlow + "\\ServiceSequanceDiagram.vsdx"; sequenceVisio = visioApp.Documents.Open(sequenceDiagramPath); Visio.Page sequencePage = sequenceVisio.Pages[1]; foreach (Visio.Shape shp in sequencePage.Shapes) { if (shp.Name != "Watermark Title" && shp.Text != "") { shp.Text = shp.Text.Replace("ServiceCanonicalName", Constants.ServiceCanonicalName); shp.Text = shp.Text.Replace("Subject", Constants.Subject); shp.Text = shp.Text.Replace("Backend", Constants.BackendName); } } Visio.Selection sequenceDiagram = sequencePage.CreateSelection(Visio.VisSelectionTypes.visSelTypeAll); sequenceDiagram.Copy(); //rollback foreach (Visio.Shape shp in sequencePage.Shapes) { if (shp.Name != "Watermark Title" && shp.Text != "") { shp.Text = shp.Text.Replace(Constants.ServiceCanonicalName, "ServiceCanonicalName"); shp.Text = shp.Text.Replace(Constants.Subject, "Subject"); shp.Text = shp.Text.Replace(Constants.BackendName, "Backend"); } } sequenceVisio.Saved = true; Word.Bookmark sequenceDiagBM = doc.Bookmarks["SequenceDiagram"]; Word.Range sequenceDiagRng = sequenceDiagBM.Range; sequenceDiagRng.PasteAndFormat(Word.WdRecoveryType.wdFormatOriginalFormatting); Console.WriteLine("Sequance Diagram Added"); } catch (Exception ex) { throw ex; } }
public void addRequestFlowDiagram(ref Word.Document doc) { try { Visio.Document requestVisio; string RequestDiagramPath = Directory.GetCurrentDirectory() + "\\Templates\\" + Constants.ServiceFlow + "\\ServiceRequestFlow.vsdx"; requestVisio = visioApp.Documents.Open(RequestDiagramPath); Visio.Page requestPage = requestVisio.Pages[1]; foreach (Visio.Shape shp in requestPage.Shapes) { if (shp.Name != "Watermark Title" && shp.Text != "") { shp.Text = shp.Text.Replace("ServiceCanonicalName", Constants.ServiceCanonicalName); } } Visio.Selection requestDiagram = requestPage.CreateSelection(Visio.VisSelectionTypes.visSelTypeAll); requestDiagram.Copy(); //rollback foreach (Visio.Shape shp in requestPage.Shapes) { if (shp.Name != "Watermark Title" && shp.Text != "") { shp.Text = shp.Text.Replace(Constants.ServiceCanonicalName, "ServiceCanonicalName"); } } requestVisio.Saved = true; Word.Bookmark requestDiagBM = doc.Bookmarks["RequestFlow"]; Word.Range requestDiagRng = requestDiagBM.Range; requestDiagRng.PasteAndFormat(Word.WdRecoveryType.wdFormatOriginalFormatting); Console.WriteLine("Request flow Added"); } catch (Exception ex) { throw ex; } }
public static void init_ListOfE_New(Word.Document RealDoc) { // JUST FOR ADD PAGES TO //BuildTable_ListOfEffctive_BigData(RealDoc, RealDoc.Tables[2]); // return; //END OF BIG DATA Object SelectionNext; DocSettings DS = new DocSettings(RealDoc); SelectionNext = RealDoc.Application.Selection.End; Word.Range rangeForToCopy = RealDoc.Range(ref SelectionNext, ref SelectionNext); int pageSize = 4; if (rangeForToCopy.PageSetup.PaperSize == Word.WdPaperSize.wdPaperA5) { pageSize = 5; } int currentPageNum = Convert.ToInt32(RealDoc.Application.Selection.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdActiveEndPageNumber)); object currentPageNumToRef = currentPageNum; Word.Document DocOfE = null; try { DocOfE = Globals.ThisAddIn.Application.Documents.Add(LOEPPath(pageSize)); } catch (Exception ex) { MessageBox.Show("Something Went Wrong - " + ex.Message); OpenFileDialog openFile = new OpenFileDialog(); openFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); openFile.Title = "Select Word Template"; openFile.FileName = ""; openFile.Filter = "Word Documents (*.doc;*.docx)|*.doc;*.docx"; if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK) { DocOfE = Globals.ThisAddIn.Application.Documents.Add(openFile.FileName); } else { settings.alert.Close(); } } Cursor.Current = Cursors.WaitCursor; DocOfE.Tables[1].Range.Copy(); rangeForToCopy.PasteAndFormat(Word.WdRecoveryType.wdFormatOriginalFormatting); try { int DocPageNumber = DS.GetPageNumber(RealDoc); RealDoc.Activate(); int FirstHeaderPage = 0; int pageToDo = BuildTable_ListOfEffctivePage(DS, rangeForToCopy.Tables[1], DocPageNumber, FirstHeaderPage); copyCellsToTable(rangeForToCopy.Tables[1], DocOfE.Tables[2], (DocPageNumber + pageToDo - FirstHeaderPage), FirstHeaderPage); DocOfE.Close(ref saveOption, ref originalFormat, ref routeDocument); if (pageToDo > 0) { try { string PageString = RealDoc.Variables["edocs_Page" + currentPageNum + "_page"].Value; string PageRev = RealDoc.Variables["edocs_Page" + PageString + "_rev"].Value; string PageDate = RealDoc.Variables["edocs_Page" + PageString + "_date"].Value; for (int i = currentPageNum + 1; i <= currentPageNum + pageToDo; i++) { string PageString2 = RealDoc.Variables["edocs_Page" + i + "_page"].Value; RealDoc.Variables["edocs_Page" + PageString2 + "_rev"].Value = PageRev; RealDoc.Variables["edocs_Page" + PageString2 + "_date"].Value = PageDate; } } catch { } } try { string test1 = RealDoc.Variables["processType"].Value; } catch { RealDoc.Variables.Add("processType", "styles"); } if (pageToDo == -1 || !DS.PageNumberFromHeaders(DocPageNumber + pageToDo, RealDoc.Variables["processType"].Value)) { RealDoc.Application.ScreenUpdating = true; } RealDoc.Application.Selection.GoTo(ref what, ref which, ref currentPageNumToRef, ref missing); trackChange(RealDoc, true); } catch (Exception ex) { trackChange(RealDoc, true); RealDoc.Application.ScreenUpdating = true; MessageBox.Show("Something Went Wrong - " + ex.Message); settings.alert.Close(); } Cursor.Current = Cursors.Default; rangeForToCopy.Tables[1].Range.Fields.Locked = 0; rangeForToCopy.Tables[1].Range.Fields.Update(); DS.UpDateFields(); }