Word VSTO - 如何填充形状效果?
Word VSTO - How to fill effects on shape?
我使用下面的代码在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 并使用 Fill Effects 通过代码添加图片。如何实现?
恐怕以下内容是在 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
等。上述方法之一应该可以解决问题。
我使用下面的代码在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 并使用 Fill Effects 通过代码添加图片。如何实现?
恐怕以下内容是在 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
等。上述方法之一应该可以解决问题。