我使用以下代码将形状(矩形)插入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
但是这会将形状添加到当前页面的顶部。我想在光标点添加形状。怎么做?
答案 0 :(得分:1)
您需要在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);
}
希望这会有所帮助: - )