繁体   English   中英

Word VSTO-在光标处插入形状?

[英]Word VSTO - Insert shape at cursor point?

我使用以下代码在Word文档中插入形状(矩形),

Dim oShpWidth As Single
Dim oShpHght As Single
Dim oShpTop As Single
Dim oShpLeft As Single
With Globals.ThisAddIn.Application.ActiveDocument
    oShpWidth = 225.1
        oShpHght = 224.5
        oShpTop = 0
        oShpLeft = 0
        .Shapes.AddShape(1, 0, 0, oShpWidth, oShpHght).Select()

        With Globals.ThisAddIn.Application.Selection.ShapeRange
            .Rotation = 0.0#
                .RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionCharacter
                .RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionLine
                .Left = oShpTop
                .Top = oShpLeft
        End With
End With

但这会将形状添加到当前页面的顶部。 我想在光标处添加形状。 怎么做?

您需要在Selection添加形状尝试以下操作:

using System;
using Word = Microsoft.Office.Interop.Word;
using System.Drawing;


namespace Sk.WordAddIn
{
    internal class ImageTest
    {

        internal void InsertShapeAtSelection()
        {
            Word.Application app = Globals.ThisAddIn.Application;
            Word.Document doc = app.ActiveDocument;

            Word.InlineShape shape = app.Selection.InlineShapes.AddPicture(@"C:\Users\Public\Pictures\Sample Pictures\Koala.jpg", false, true, Type.Missing);
            shape.Fill.BackColor.RGB = ColorTranslator.ToOle(Color.Transparent);
            shape.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
            shape.Fill.Transparency = 0f;
            shape.Line.Transparency = 0f;
            shape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;

        }
    }
}

编辑:试试这个:

    internal void InsertShapeAtSelection()
    {
        Word.Application app = Globals.ThisAddIn.Application;
        Word.Document doc = app.ActiveDocument;
        float left = (float)Convert.ToDouble(app.Selection.Information[Word.WdInformation.wdHorizontalPositionRelativeToPage]);
        float top = (float)Convert.ToDouble(app.Selection.Information[Word.WdInformation.wdVerticalPositionRelativeToPage]);
        Word.Shape shape = doc.Shapes.AddShape(1, left, top, 225.1f, 224.5f);


        //To change borders and fill use the properties below
        //shape.Fill.BackColor.RGB = ColorTranslator.ToOle(Color.Transparent);
        //shape.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
        //shape.Fill.Transparency = 0f;
        //shape.Line.Transparency = 0f;
        //shape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;

        //To add a picture uncomment the following
        //Word.Range range1 = shape.TextFrame.TextRange;
        //range1.InlineShapes.AddPicture(@"C:\Users\Public\Pictures\Sample Pictures\Koala.jpg", false, true, Type.Missing);
    }

希望这可以帮助 :-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM