public ShapsOps(T shape, string typeName)
        {
            shapsOps = shape;
            Name     = typeName;
            switch (typeName)
            {
            case "PowerPnt.Shape":
                pptShape = shape as PowerPnt.Shape;
                //shapsOps = pptShape;
                break;

            case "WinWord.Shape":
                docShape = shape as WinWord.Shape;
                //shapsOps = docShape;
                break;

            case "PowerPnt.ShapeRange":
                pptShapeRng = (PowerPnt.ShapeRange)shape;
                //shapsOps = pptShapeRng;
                break;

            case "WinWord.ShapeRange":
                docShapeRng = (WinWord.ShapeRange)shape;
                //shapsOps = docShapeRng;
                break;

            default:
                break;
            }
        }
Example #2
0
        public void StartSpellChecking()
        {
            Cursor.Current = Cursors.WaitCursor;

            Globals.ThisAddIn.SpellOn = true;
            ResetAllInitVariables();

            if (!tabControl.TabPages.Contains(tabPage1))
            {
                tabControl.TabPages.Add(tabPage1);
            }
            tabControl.SelectTab(tabPage1);

            m_Inspector = Globals.ThisAddIn.Application.ActiveInspector();
            m_Document  = m_Inspector.WordEditor;
            if (!m_Inspector.IsWordMail())
            {
                m_Document = m_Inspector.CurrentItem as Word.Document;
            }

            // get the cursor position in the document
            nCursorPos = m_Document.Application.Selection.Start;
            // get all shapes in the document
            oShapes = m_Document.Application.ActiveDocument.Shapes;

            Word.ShapeRange oShpRng = m_Document.Application.Selection.ShapeRange;
            if (oShpRng.Count > 0)
            {
                object     obj = 1;
                Word.Shape shp = oShpRng.get_Item(ref obj);
                bCheckNormalText = false;
                bCursorInShape   = true;

                for (int i = 1; i <= oShapes.Count; i++)
                {
                    obj         = i;
                    nShapeIndex = i;
                    Word.Shape oShp = oShapes.get_Item(ref obj);
                    if (oShp.Name == shp.Name)
                    {
                        break;
                    }
                }
            }
            RestartParagraph();
            SetSpellingState("Անտեսել");
        }
Example #3
0
        /// <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;
            }
        }
Example #4
0
        /// <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文档中没有可以进行数据提取的图表");
            }
        }