Codename K
Codename K

Reputation: 744

Word VSTO - How to fill effects on shape?

I use the following code to insert a shape (rectangle) to the Word document,

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

But I also like to change the back color to No Color, line color to No Color and use Fill Effects to add a picture via code. How to achieve this?

Upvotes: 2

Views: 1867

Answers (1)

Charlie
Charlie

Reputation: 376

I'm afraid that the following is in C# but you should be able to convert to VB.Net fairly easily.

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


namespace WordAddIn
{
    public class ImageTest
    {
       internal void InsertTextBoxWithPicture()
       {
           Word.Application app = Globals.ThisAddIn.Application;
           Word.Document doc = app.ActiveDocument;

           Word.Shape shape1 = doc.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, app.CentimetersToPoints(2.45f),
            app.CentimetersToPoints(1.25f), app.CentimetersToPoints(9.2f), app.CentimetersToPoints(2.5f));
           shape1.TextFrame.MarginLeft = 0f;
           shape1.TextFrame.MarginRight = 0f;
           shape1.TextFrame.MarginTop = 0f;
           shape1.TextFrame.MarginBottom = 0f;
           shape1.Fill.BackColor.RGB = ColorTranslator.ToOle(Color.Transparent);
           shape1.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
           shape1.Fill.Transparency = 0f;
           shape1.Line.Transparency = 0f;
           shape1.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
           Word.Range range = shape1.TextFrame.TextRange;
           range.InlineShapes.AddPicture(@"C:\Users\Public\Pictures\Sample Pictures\Koala.jpg", false, true, Type.Missing);
       }

       internal void InsertShapeWithPicture()
       {
           Word.Application app = Globals.ThisAddIn.Application;
           Word.Document doc = app.ActiveDocument;
           Word.Shape shape = doc.Shapes.AddShape(1, 0f, 0f, 225.1f, 224.5f);
           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;

           Word.Range range1 = shape.TextFrame.TextRange;
           range1.InlineShapes.AddPicture(@"C:\Users\Public\Pictures\Sample Pictures\Koala.jpg", false, true, Type.Missing);

        }
    }
}

In C# a float has to be explicitly declared hence 0f etc. One of the above methods should do the trick.

Upvotes: 1

Related Questions