private void ExtractShapeContents(List <string> contents, Word.Shape shape) { shape.Select();//shape.Type fails if not selected. This problem is word only. if (shape.Type == Microsoft.Office.Core.MsoShapeType.msoGroup) { //To check group or not, use only shape.AutoShapeType == msoShapeMixed or shape.Type == msoGroup, //because other ways like shape.GroupItem.Count & shape.Ungroup thow an exception when shape is not a group. foreach (Word.Shape subShape in shape.GroupItems) { ExtractShapeContents(contents, subShape); } } else if (shape.Type == Microsoft.Office.Core.MsoShapeType.msoCanvas) { foreach (Word.Shape subShape in shape.CanvasItems) { ExtractShapeContents(contents, subShape); } } else { if (shape.TextFrame != null && shape.TextFrame.HasText != 0) { var text = shape.TextFrame?.TextRange?.Text; if (!String.IsNullOrEmpty(text)) { contents.Add(text); } } } }
public static void SaveShapeToFile(Word.Shape shape) { if (shape == null) { return; } shape.Select(); Globals.ThisAddIn.Application.Selection.Copy(); if (System.Windows.Forms.Clipboard.GetDataObject() != null) { var data = System.Windows.Forms.Clipboard.GetDataObject(); if (data != null && data.GetDataPresent(System.Windows.Forms.DataFormats.Bitmap)) { System.Drawing.Image img = (System.Drawing.Image)data.GetData(System.Windows.Forms.DataFormats.Bitmap, true); img.Save(@"d:\TT.jpg"); } } }
/// <summary> /// 将Shape对象转换为Image /// </summary> /// <param name="shape"></param> /// <returns></returns> private System.Drawing.Image ConvertToImage(Word.Shape shape) { if (shape == null) { return(null); } shape.Select(); m_Doc.Application.Selection.Copy(); //Globals.ThisAddIn.Application.Selection.Copy(); if (System.Windows.Forms.Clipboard.GetDataObject() != null) { var data = System.Windows.Forms.Clipboard.GetDataObject(); if (data != null && data.GetDataPresent(System.Windows.Forms.DataFormats.Bitmap)) { System.Drawing.Image img = (System.Drawing.Image)data.GetData(System.Windows.Forms.DataFormats.Bitmap, true); return(img); } } return(null); }