//批量模式按钮 VVV private void btn_nxtTeX_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Word.Document ThisDoc = Globals.ThisAddIn.Application.ActiveDocument; Microsoft.Office.Interop.Word.Range TexItem = null; ThisDoc.Application.Selection.SetRange(ThisDoc.Application.Selection.Start, ThisDoc.Application.Selection.End + 1); TexItem = ThisDoc.Application.Selection.GoToNext(WdGoToItem.wdGoToGraphic); TexItem.SetRange(TexItem.Start, TexItem.End + 1); TexItem.Select(); InlineShapes SelectedObj = ThisDoc.Application.Selection.InlineShapes; InlineShape SelectedObjFirst = SelectedObj[1]; if (ThisDoc == null || ThisDoc.ReadOnly) { return; } if (SelectedObj.Count == 0) { return; } if (!SelectedObjFirst.AlternativeText.Contains("WordxTex_TexContent")) { return; } texCodeBox.Clear(); texCodeBox.Text = SelectedObjFirst.AlternativeText; }
private void ReplaceShape(Application app, string newFilePath) { Console.WriteLine("Starting At:" + DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture)); var activeDocument = app.ActiveDocument; activeDocument.Activate(); InlineShapes shapes = activeDocument.InlineShapes; Console.WriteLine("InlineShapesCount::" + shapes.Count); var counter = 100; foreach (InlineShape oWordShape in shapes) { counter++; if (oWordShape.AlternativeText != null && oWordShape.AlternativeText.Contains(counter.ToString())) { Console.WriteLine("Index:" + counter + "||Start::ReplaceImageinInlineShapeAt:" + DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture)); object rangeAddress = oWordShape.Range; var newShape = app.ActiveDocument.InlineShapes.AddPicture(newFilePath, ref MissingType, ref MissingType, ref rangeAddress); newShape.AlternativeText = oWordShape.AlternativeText; oWordShape.Delete(); Console.WriteLine("Index:" + counter + "End::ReplaceImageinInlineShapeAt:" + DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture)); } } }
private void Application_WindowBeforeDoubleClick(Selection Sel, ref bool Cancel) { InlineShapes SelectedObj = Sel.InlineShapes; if (SelectedObj.Count == 0) { return; } InlineShape SelectedObjFirst = SelectedObj[1]; if (!SelectedObjFirst.AlternativeText.Contains("WordxTex_TexContent")) { return; } LaTexEdt CodeEditor = new LaTexEdt(false, SelectedObjFirst.AlternativeText, 0, 0); CodeEditor.ShowDialog(); }
public void SaveToWord(string image) { if (this.wordDocument != null) { object obj = WdGoToItem.wdGoToLine; object obj1 = WdGoToDirection.wdGoToLast; Document document = this.wordDocument; object missing = Type.Missing; object missing1 = Type.Missing; document.GoTo(ref obj, ref obj1, ref missing, ref missing1); Selection selection = this.WordApp.Selection; object obj2 = WdUnits.wdStory; object value = System.Reflection.Missing.Value; selection.EndKey(ref obj2, ref value); InlineShapes inlineShapes = this.wordDocument.InlineShapes; object missing2 = Type.Missing; object missing3 = Type.Missing; object obj3 = Type.Missing; inlineShapes.AddPicture(image, ref missing2, ref missing3, ref obj3); Document document1 = this.wordDocument; object obj4 = this.documentPath; object missing4 = Type.Missing; object missing5 = Type.Missing; object obj5 = Type.Missing; object missing6 = Type.Missing; object obj6 = Type.Missing; object missing7 = Type.Missing; object obj7 = Type.Missing; object missing8 = Type.Missing; object obj8 = Type.Missing; object missing9 = Type.Missing; object obj9 = Type.Missing; object missing10 = Type.Missing; object obj10 = Type.Missing; object missing11 = Type.Missing; object obj11 = Type.Missing; document1.SaveAs(ref obj4, ref missing4, ref missing5, ref obj5, ref missing6, ref obj6, ref missing7, ref obj7, ref missing8, ref obj8, ref missing9, ref obj9, ref missing10, ref obj10, ref missing11, ref obj11); } }
protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state) { PropertyDescriptor property = context.DataContext.GetProperties()[WordCreate.GetWordAppTag]; Application wordApp = property.GetValue(context.DataContext) as Application; try { string linkName = LinkName.Get(context); string linkMark = LinkMark.Get(context); string linkAddr = LinkAddr.Get(context); string bookMark = BookMark.Get(context); string pic = Pic.Get(context); if (linkName != null) { Hyperlinks links = wordApp.Selection.Hyperlinks; links.Add(wordApp.Selection.Range, linkAddr, linkMark, "", linkName, linkMark); } if (bookMark != null) { Bookmarks bookmarks = wordApp.Selection.Bookmarks; bookmarks.Add(bookMark); } if (pic != null) { InlineShapes lineshapes = wordApp.Selection.InlineShapes; InlineShape lineshape = lineshapes.AddPicture(pic); } } catch (Exception e) { SharedObject.Instance.Output(SharedObject.enOutputType.Error, "Word执行过程出错", e.Message); CommonVariable.realaseProcessExit(wordApp); } m_Delegate = new runDelegate(Run); return(m_Delegate.BeginInvoke(callback, state)); }
private void btn_edit_Click(object sender, RibbonControlEventArgs e) { Microsoft.Office.Interop.Word.Document ThisDoc = Globals.ThisAddIn.Application.ActiveDocument; if (ThisDoc == null || ThisDoc.ReadOnly) { return; } InlineShapes SelectedObj = ThisDoc.Application.Selection.InlineShapes; if (SelectedObj.Count == 0) { return; } InlineShape SelectedObjFirst = SelectedObj[1]; if (!SelectedObjFirst.AlternativeText.Contains("WordxTex_TexContent")) { return; } LaTexEdt CodeEditor = new LaTexEdt(false, SelectedObjFirst.AlternativeText, 0, 0); CodeEditor.ShowDialog(); }
public void ThisDocument_SelectionChange(object sender, Microsoft.Office.Tools.Word.SelectionEventArgs e) { if (TaskPane.Visible) {//only bother updating if there's something to put data into. if (e.Selection.Type == WdSelectionType.wdSelectionInlineShape) { InlineShapes shapes = e.Selection.InlineShapes; if (shapes.Count > 0) { //InlineShape selectedShape; foreach (InlineShape shape in shapes) { //selectedShape = shape; userControl.Diagram = UmlDiagram.parseAltText(shape.AlternativeText); } } } else { userControl.Diagram = null; } } }
internal override void Dispose(bool disposing) { if (disposing) { // Free managed if (_contentControls != null && !_contentControls.IsDisposed) { _contentControls.Dispose(); _contentControls = null; } if (_fields != null && !_fields.IsDisposed) { _fields.Dispose(); _fields = null; } if (_inlineShapes != null && !_inlineShapes.IsDisposed) { _inlineShapes.Dispose(); _inlineShapes = null; } } base.Dispose(true); }
/// <summary> /// 规范表格,而且删除表格中的嵌入式图片 /// </summary> /// <param name="TableStyle">要应用的表格样式</param> /// <param name="ParagraphFormat">表格中的段落样式</param> /// <param name="blnDeleteShapes">是否要删除表格中的图片,包括嵌入式或非嵌入式图片。</param> /// <remarks></remarks> public void TableFormat(string TableStyle = "zengfy表格-上下总分型1", string ParagraphFormat = "表格内容置顶", bool blnDeleteShapes = false) { var Selection = _app.Selection; if (Selection.Tables.Count > 0) { //定位表格 Table Tb = default(Table); Range rg = default(Range); foreach (Table tempLoopVar_Tb in Selection.Range.Tables) { Tb = tempLoopVar_Tb; rg = Tb.Range; _app = Tb.Application; // _app.ScreenUpdating = false; //调整表格尺寸 dynamic with_1 = Tb; with_1.AutoFitBehavior(WdAutoFitBehavior.wdAutoFitContent); with_1.AutoFitBehavior(WdAutoFitBehavior.wdAutoFitWindow); //清除表格中的超链接 Hyperlinks hps = default(Hyperlinks); hps = rg.Hyperlinks; int hpCount = 0; hpCount = hps.Count; for (var i = 1; i <= hpCount; i++) { hps[1].Delete(); } //将手动换行符删除 { Tb.Range.Find.ClearFormatting(); Tb.Range.Find.Replacement.ClearFormatting(); Tb.Range.Find.Text = "^l"; Tb.Range.Find.Replacement.Text = ""; Tb.Range.Find.Execute(WdReplace.wdReplaceAll); } //删除表格中的乱码空格 { Tb.Range.Find.ClearFormatting(); Tb.Range.Find.Replacement.ClearFormatting(); Tb.Range.Find.Text = " "; Tb.Range.Find.Replacement.Text = " "; Tb.Range.Find.Execute(WdReplace.wdReplaceAll); } //删除表格中的嵌入式图片 if (blnDeleteShapes) { InlineShapes inlineshps = default(InlineShapes); int Count = 0; InlineShape inlineShp = default(InlineShape); inlineshps = Tb.Range.InlineShapes; Count = inlineshps.Count; for (var i = Count; i >= 1; i--) { inlineShp = inlineshps[Convert.ToInt32(i)]; inlineShp.Delete(); } //删除表格中的图片 ShapeRange shps = default(ShapeRange); Shape shp = default(Shape); shps = Tb.Range.ShapeRange; Count = shps.Count; for (var i = Count; i >= 1; i--) { shp = shps[i]; shp.Delete(); } } //清除表格中的格式设置 rg.Select(); Selection.ClearFormatting(); // ----- 设置表格样式与表格中的段落样式 try //设置表格样式 { Tb.set_Style(TableStyle); } catch (Exception) { } try //设置表格中的段落样式 { rg.ParagraphFormat.set_Style(ParagraphFormat); } catch (Exception) { } } //取消选择并刷新界面 Selection.Collapse(); _app.ScreenRefresh(); _app.ScreenUpdating = true; } else { MessageBox.Show("请至少选择一个表格。", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } }
/// <summary> /// 提取Word中的图表中的数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> /// <remarks>如果将Excel中的Chart粘贴进Word,而且是以链接的形式粘贴的。在后期操作中,此Chart所链接的源Excel文件丢失,此时在Word中便不能直接提取到Excel中的数据了。</remarks> public void ExtractDataFromWordChart(object sender, RibbonControlEventArgs e) { Chart cht = null; Selection sele = _app.Selection; //先查看文档中有没有InlineShape类型的Chart InlineShapes ilshps = default(InlineShapes); InlineShape ilshp = default(InlineShape); ilshps = sele.InlineShapes; foreach (InlineShape tempLoopVar_ilshp in ilshps) { ilshp = tempLoopVar_ilshp; if (ilshp.HasChart == Office.MsoTriState.msoTrue) { cht = ilshp.Chart; break; } } //再查看文档中有没有Shape类型的Chart(即不是嵌入式图形的Chart,而是浮动式图形) if (cht == null) { ShapeRange shps = default(ShapeRange); Shape shp = default(Shape); shps = sele.ShapeRange; foreach (Shape tempLoopVar_shp in shps) { shp = tempLoopVar_shp; if (shp.HasChart == Office.MsoTriState.msoTrue) { cht = shp.Chart; break; } } } //对Chart中的数据进行提取 if (cht != null) { // string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory, Environment.SpecialFolderOption.None); var ExcelFilePath = Path.Combine(new[] { desktopPath, "Word 图表数据.xlsx" }); //用来保存数据的Excel工作簿的路径。 Microsoft.Office.Interop.Excel.Application ExcelApp = default(Microsoft.Office.Interop.Excel.Application); Workbook Wkbk = default(Workbook); Worksheet sht = default(Worksheet); bool blnExcelFileExists = false; //此Excel工作簿是否存在 if (File.Exists(ExcelFilePath.ToString())) { blnExcelFileExists = true; // 直接打开外部的文档 Wkbk = (Workbook)Interaction.GetObject(ExcelFilePath.ToString(), null); // 打开一个Excel文档,以保存Word图表中的数据 ExcelApp = Wkbk.Application; } else { // 先创建一个Excel进程,然后再在其中添加一个工作簿。 ExcelApp = new Microsoft.Office.Interop.Excel.Application(); Wkbk = ExcelApp.Workbooks.Add(); } sht = Wkbk.Worksheets[1]; // 用工作簿中的第一个工作表来存放数据。 sht.UsedRange.Value = null; // SeriesCollection seriesColl = (SeriesCollection)cht.SeriesCollection(); //这里不能定义其为Excel.SeriesCollection Series Chartseries; //开始提取数据 short col = (short)1; dynamic X = default(dynamic); // 这里只能将X与Y的数据类型定义为Object,不能是Object()或者Object(,) object Y = null; string Title = ""; // 这里不能用For Each Chartseries in SeriesCollection来引用seriesCollection集合中的元素。 for (var i = 1; i <= seriesColl.Count; i++) { // 在VB.NET中,seriesCollection集合中的第一个元素的下标值为1。 Chartseries = seriesColl[i]; X = Chartseries.XValues; Y = Chartseries.Values; Title = Chartseries.Name; // 将数据存入Excel表中 int PointsCount = Convert.ToInt32(X.Length); if (PointsCount > 0) { sht.Cells[1, col].Value = Title; sht.Range[sht.Cells[2, col], sht.Cells[PointsCount + 1, col]].Value = ExcelApp.WorksheetFunction.Transpose(X); sht.Range[sht.Cells[2, col + 1], sht.Cells[PointsCount + 1, col + 1]].Value = ExcelApp.WorksheetFunction.Transpose(Y); col = (short)(col + 3); } } if (blnExcelFileExists) { Wkbk.Save(); } else { Wkbk.SaveAs(Filename: ExcelFilePath); } sht.Activate(); ExcelApp.Windows[Wkbk.Name].Visible = true; //取消窗口的隐藏 ExcelApp.Windows[Wkbk.Name].Activate(); ExcelApp.Visible = true; if (ExcelApp.WindowState == XlWindowState.xlMinimized) { ExcelApp.WindowState = XlWindowState.xlNormal; } } else { MessageBox.Show("此Word文档中没有可以进行数据提取的图表"); } }
private void btnCreateIssue_Click(object sender, RibbonControlEventArgs e) { application = Globals.ThisAddIn.Application; Word.Selection selection = application.Selection; string selectionText = selection.Text; // TODO: we may need to clean things up, or iterate through each line as an issue? if (String.IsNullOrWhiteSpace(selectionText)) { MessageBox.Show("Text is required to create an issue."); return; } selectionText = System.Text.RegularExpressions.Regex.Replace(selectionText, @"\t|\n|\r", ""); while (selectionText.EndsWith("/")) { selectionText = selectionText.Remove(selectionText.Length - 1); } InlineShapes inlineShapes = selection.InlineShapes; List <System.Drawing.Image> images = new List <System.Drawing.Image>(); foreach (InlineShape inlineShape in inlineShapes) { inlineShape.Select(); // TODO: this resets the selection, which prevents multiple image uploads. selection.Copy(); if (System.Windows.Forms.Clipboard.GetDataObject() != null) { System.Windows.Forms.IDataObject data = System.Windows.Forms.Clipboard.GetDataObject(); if (data.GetDataPresent(System.Windows.Forms.DataFormats.Bitmap)) { System.Drawing.Image image = (System.Drawing.Image)data.GetData(System.Windows.Forms.DataFormats.Bitmap, true); images.Add(image); } else { System.Windows.Forms.MessageBox.Show("The Data In Clipboard is not as image format"); } } else { // MessageBox.Show("The Clipboard was empty"); } } // get currently selected project id int projectId = api.Projects.Find(x => x.Name == drpDwnProjects.SelectedItem.Label).Id; var createdIssue = api.CreateIssue(selectionText, api.HubId, projectId); foreach (System.Drawing.Image image in images) { api.CreateViewpoint(api.HubId, projectId, createdIssue.Id, image); image.Dispose(); } // https://[hubName].bimtrackapp.co/Projects/{projectId}/Issues/{issueNumber} // Convert selected text to link to newly created issue. // TODO: link creation is broken since I added the image. Object hyperlink = $"https://{api.HubName}.bimtrackapp.co/Projects/" + createdIssue.ProjectId + "/Issues/" + createdIssue.Number; Object oRange = selection.Range; selection.Hyperlinks.Add(oRange, ref hyperlink); }