【问题标题】:Word VSTO - How to fill effects on shape?Word VSTO - 如何填充形状效果?
【发布时间】:2015-07-20 15:31:03
【问题描述】:

我使用下面的代码在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

但我也喜欢将背景颜色更改为 No Color,线条颜色更改为 No Color 和使用填充效果通过代码添加图片。如何做到这一点?

【问题讨论】:

    标签: vb.net ms-word vsto shape fill


    【解决方案1】:

    恐怕以下内容是用 C# 编写的,但您应该能够相当轻松地转换为 VB.Net。

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

    在 C# 中,必须显式声明浮点数,因此 0f 等。上述方法之一应该可以解决问题。

    【讨论】:

    猜你喜欢
    • 2015-10-13
    • 1970-01-01
    • 2017-03-13
    • 2017-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    相关资源
    最近更新 更多