在 VBA for Word 中添加形状,定义大小、页面位置和颜色

问题描述 投票:0回答:1

我对 VBA 完全陌生,正在尝试从已经发布的示例中学习,但需要专家的一点帮助。

我正在尝试创建 Word 模板,该模板显示灰色侧边栏,并在每个页面上显示说明文本。当用户创建新页面时,我想为他们提供一个宏,可以轻松地将相同的侧边栏放置在新页面上。

这是我到目前为止所能找到的:

Sub Sidebar() 
Application.ScreenUpdating = False
Dim Shp As Shape, Rng As Range, Hght As Single, Wdth As Single
Hght = InchesToPoints(11): Wdth = InchesToPoints(2.17)
With ActiveDocument
    Set Shp = .Shapes.AddShape(Type:=msoShapeRectangle, _
    Left:=RelativeVerticalPosition = 0, _
    Top:=RelativeHorizontalPosition = 0 - Hght, _
    Width:=Wdth, Height:=Hght)
End With
Application.ScreenUpdating = True
End Sub`

它将侧边栏完美地放置在具有正确尺寸的正确位置。然而,根据我的主题颜色,它是绿色。如何更改代码以更改填充和轮廓颜色?

colors format shapes
1个回答
0
投票

这对于评论来说太长了,而且难以辨认,因此构成了对原始问题的部分答案,并选择了初始化的正确结构(记录为 Macro1)。代码中的断点应该允许您查找想要更改的任何其他设置。

Sub Macro1()
'
' Macro1 Macro 
'  
  ActiveSheet.Shapes.AddShape(msoShapeRectangle, 646.5, 149.25, 178.5, 88.5).Select
  With Selection.ShapeRange.Fill
     .Visible = msoTrue
     .ForeColor.RGB = RGB(255, 255, 0) '' pale grey .RGB(192,192,192)
     .Transparency = 0
     .Solid
  End With
  With Selection.ShapeRange.Line
    .Visible = msoTrue
    .ForeColor.RGB = RGB(112, 48, 160)
    .Transparency = 0
  End With
  Selection.ShapeRange.IncrementLeft 18
  Selection.ShapeRange.IncrementTop 10.5
  Selection.ShapeRange.IncrementRotation 25.3589833333
End Sub

上面是从 Office 16 直接在 Excel 上录制的宏。我想知道为什么你不能在你的系统上这样做。我在设置中进行了快速搜索,但没有看到任何明显会妨碍宏录制的东西。

© www.soinside.com 2019 - 2024. All rights reserved.