C# 在光标位置插入字形状

C# 在光标位置插入字形状,c#,ms-word,office-interop,C#,Ms Word,Office Interop,我正在寻找一种在光标位置插入MS Word形状的方法。目前,我有以下代码,可在预定位置插入形状: Microsoft.Office.Interop.Word.Range CurrRange = Globals.ThisAddIn.Application.Selection.Range; //Get the id of the MS Word shape to be inserted int shapeId = (int)MsoAutoShapeT

我正在寻找一种在光标位置插入MS Word形状的方法。目前,我有以下代码,可在预定位置插入形状:

        Microsoft.Office.Interop.Word.Range CurrRange = Globals.ThisAddIn.Application.Selection.Range;

        //Get the id of the MS Word shape to be inserted
        int shapeId = (int)MsoAutoShapeType.msoShapeRoundedRectangle;
        //Get the value of the name attribute from the selected tree view item
        string nodeText = treeViewItem.GetAttribute("name");

        //Add a new shape to the MS Word designer and set shape properties
        var shape = CurrRange.Document.Shapes.AddShape(shapeId, 170, 200, 100, 20);
        shape.AlternativeText = String.Format("Alt {0}", nodeText);
        shape.TextFrame.ContainingRange.Text = nodeText;
        shape.TextFrame.ContainingRange.Font.Size = 8;
插入形状的位置是硬编码的:

        Microsoft.Office.Interop.Word.Range CurrRange = Globals.ThisAddIn.Application.Selection.Range;

        //Get the id of the MS Word shape to be inserted
        int shapeId = (int)MsoAutoShapeType.msoShapeRoundedRectangle;
        //Get the value of the name attribute from the selected tree view item
        string nodeText = treeViewItem.GetAttribute("name");

        //Add a new shape to the MS Word designer and set shape properties
        var shape = CurrRange.Document.Shapes.AddShape(shapeId, 170, 200, 100, 20);
        shape.AlternativeText = String.Format("Alt {0}", nodeText);
        shape.TextFrame.ContainingRange.Text = nodeText;
        shape.TextFrame.ContainingRange.Font.Size = 8;
这可以从
AddShape()
方法的第2和第3个参数中看出:

        Microsoft.Office.Interop.Word.Range CurrRange = Globals.ThisAddIn.Application.Selection.Range;

        //Get the id of the MS Word shape to be inserted
        int shapeId = (int)MsoAutoShapeType.msoShapeRoundedRectangle;
        //Get the value of the name attribute from the selected tree view item
        string nodeText = treeViewItem.GetAttribute("name");

        //Add a new shape to the MS Word designer and set shape properties
        var shape = CurrRange.Document.Shapes.AddShape(shapeId, 170, 200, 100, 20);
        shape.AlternativeText = String.Format("Alt {0}", nodeText);
        shape.TextFrame.ContainingRange.Text = nodeText;
        shape.TextFrame.ContainingRange.Font.Size = 8;
170=在自选图形左边缘的点上测量的位置

        Microsoft.Office.Interop.Word.Range CurrRange = Globals.ThisAddIn.Application.Selection.Range;

        //Get the id of the MS Word shape to be inserted
        int shapeId = (int)MsoAutoShapeType.msoShapeRoundedRectangle;
        //Get the value of the name attribute from the selected tree view item
        string nodeText = treeViewItem.GetAttribute("name");

        //Add a new shape to the MS Word designer and set shape properties
        var shape = CurrRange.Document.Shapes.AddShape(shapeId, 170, 200, 100, 20);
        shape.AlternativeText = String.Format("Alt {0}", nodeText);
        shape.TextFrame.ContainingRange.Text = nodeText;
        shape.TextFrame.ContainingRange.Font.Size = 8;
200=以点为单位测量到自选图形上边缘的位置

        Microsoft.Office.Interop.Word.Range CurrRange = Globals.ThisAddIn.Application.Selection.Range;

        //Get the id of the MS Word shape to be inserted
        int shapeId = (int)MsoAutoShapeType.msoShapeRoundedRectangle;
        //Get the value of the name attribute from the selected tree view item
        string nodeText = treeViewItem.GetAttribute("name");

        //Add a new shape to the MS Word designer and set shape properties
        var shape = CurrRange.Document.Shapes.AddShape(shapeId, 170, 200, 100, 20);
        shape.AlternativeText = String.Format("Alt {0}", nodeText);
        shape.TextFrame.ContainingRange.Text = nodeText;
        shape.TextFrame.ContainingRange.Font.Size = 8;

我已经查看了Range对象的属性和方法,但似乎找不到任何存储我需要的位置值的属性和方法。

AddShape的最后一个参数是锚点,它需要一个Range对象。尝试将您的射程传递到以下位置:

        Microsoft.Office.Interop.Word.Range CurrRange = Globals.ThisAddIn.Application.Selection.Range;

        //Get the id of the MS Word shape to be inserted
        int shapeId = (int)MsoAutoShapeType.msoShapeRoundedRectangle;
        //Get the value of the name attribute from the selected tree view item
        string nodeText = treeViewItem.GetAttribute("name");

        //Add a new shape to the MS Word designer and set shape properties
        var shape = CurrRange.Document.Shapes.AddShape(shapeId, 170, 200, 100, 20);
        shape.AlternativeText = String.Format("Alt {0}", nodeText);
        shape.TextFrame.ContainingRange.Text = nodeText;
        shape.TextFrame.ContainingRange.Font.Size = 8;
var shape = CurrRange.Document.Shapes.AddShape(shapeId, 0, 0, 100, 20,CurrRange);
更新:看起来Word 2010文档中存在一个不尊重锚的问题。将文档另存为.doc文件并再次测试,如果这样做,它会将其锚定到段落开头。上面的链接只是指向microsoft论坛,我找不到该问题的connect bug报告

        Microsoft.Office.Interop.Word.Range CurrRange = Globals.ThisAddIn.Application.Selection.Range;

        //Get the id of the MS Word shape to be inserted
        int shapeId = (int)MsoAutoShapeType.msoShapeRoundedRectangle;
        //Get the value of the name attribute from the selected tree view item
        string nodeText = treeViewItem.GetAttribute("name");

        //Add a new shape to the MS Word designer and set shape properties
        var shape = CurrRange.Document.Shapes.AddShape(shapeId, 170, 200, 100, 20);
        shape.AlternativeText = String.Format("Alt {0}", nodeText);
        shape.TextFrame.ContainingRange.Text = nodeText;
        shape.TextFrame.ContainingRange.Font.Size = 8;
解决方法是根据选择的位置指定顶部和左侧:

        Microsoft.Office.Interop.Word.Range CurrRange = Globals.ThisAddIn.Application.Selection.Range;

        //Get the id of the MS Word shape to be inserted
        int shapeId = (int)MsoAutoShapeType.msoShapeRoundedRectangle;
        //Get the value of the name attribute from the selected tree view item
        string nodeText = treeViewItem.GetAttribute("name");

        //Add a new shape to the MS Word designer and set shape properties
        var shape = CurrRange.Document.Shapes.AddShape(shapeId, 170, 200, 100, 20);
        shape.AlternativeText = String.Format("Alt {0}", nodeText);
        shape.TextFrame.ContainingRange.Text = nodeText;
        shape.TextFrame.ContainingRange.Font.Size = 8;
Microsoft.Office.Interop.Word.Range CurrRange = Globals.ThisAddIn.Application.Selection.Range;

//Get the id of the MS Word shape to be inserted
int shapeId = (int)MsoAutoShapeType.msoShapeRoundedRectangle;

//Get the value of the name attribute from the selected tree view item
string nodeText = "Hello World";

var left = Globals.ThisAddIn.Application.Selection.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdHorizontalPositionRelativeToPage);
var top = Globals.ThisAddIn.Application.Selection.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdVerticalPositionRelativeToPage);

//Add a new shape to the MS Word designer and set shape properties
var shape = CurrRange.Document.Shapes.AddShape(shapeId, left, top, 100, 20);
shape.AlternativeText = String.Format("Alt {0}", nodeText);
shape.TextFrame.ContainingRange.Text = nodeText;
shape.TextFrame.ContainingRange.Font.Size = 8;

我已经试过了,它不起作用。形状仍然插入在同一位置。我用在Microsoft论坛上找到的一些信息更新了我的答案。我也在msdn上看到过这篇文章-但是左值和顶值总是需要增加70,否则形状会插入光标上方和左侧几厘米处…这非常奇怪和复杂我不太清楚为什么一定要这样做,但它似乎确实起了作用。谢谢你的努力,如回答所示