2015-07-18 106 views
0

我用下面的代碼的形狀(矩形)插入到Word文檔,Word VSTO - 在光標點插入形狀?

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 

但是,這增加了形狀到當前頁面的頂部。我想在光標點添加形狀。怎麼做?

回答

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); 
    } 

希望這有助於:-)

+0

謝謝[查理] (http://stackoverflow.com/users/4923873/charlie)。但此代碼Word.InlineShape shape = app.Selection.InlineShapes.AddPicture(@「C:\ Users \ Public \ Pictures \ Sample Pictures \ Koala.jpg」,false,true,Type.Missing);'僅適用於添加圖片對象。它不適用於像矩形一樣的形狀。你知道如何修改這個在光標點添加形狀嗎? –

+0

太棒了!完美工作。 –

+0

[查理](http://stackoverflow.com/users/4923873/charlie),請檢查這個問題:[字VSTO - 填充形狀不適用於Word 2010和Word 2013?](http:// stackoverflow .COM /問題/ 31589137 /字VSTO填充-A-形狀不 - 不工作在字2010和字-2013)。提前致謝。 –