【问题标题】:VBA Shapes.AddTextbox customize layoutVBA Shapes.AddTextbox 自定义布局
【发布时间】:2020-06-30 20:53:14
【问题描述】:

我想要一个使用 VBA 的 Excel 中的文本框,如下所示:

  • 测试框中的文本为 RGB(0,112,192)(蓝色)
  • 以 RGB (0,112,192) 为文本框的形状轮廓。
  • '1 1/2' 中线的粗细。

我已经创建了这段代码,但我不知道如何编程。


Sub TextBox()

Set myDocument = Worksheets(1)
myDocument.Shapes.AddTextBox(msoTextOrientationHorizontal, _
    100, 100, 200, 50) _
    .TextFrame.Characters.Text = "Test Box of how I would like it"
      
End Sub

谁能帮我解决这个问题?

【问题讨论】:

  • 打开宏记录器。插入一个文本框。格式化它。关闭宏记录器。看代码。您需要对其进行一些清理,但它会包含您需要的所有命令。

标签: excel vba textbox formatting format


【解决方案1】:

如果您在设置线宽、线色和文本颜色时打开宏记录器,您会找到执行此操作的代码。

With Selection.ShapeRange.Line
    .Weight = 1.5
    .Visible = msoTrue
    .ForeColor.RGB = RGB(0, 112, 192)
    .Transparency = 0
End With
With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 11).Font.Fill
    .Visible = msoTrue
    .ForeColor.RGB = RGB(0, 112, 192)
    .Transparency = 0
    .Solid
End With

【讨论】:

  • 它给出错误:对象不支持此属性Sub TextBox() Set myDocument = Worksheets(1) myDocument.Shapes.AddTextBox(msoTextOrientationHorizontal, _ 100, 100, 200, 50) _ .TextFrame.Characters.Text = "Test Box" With Selection.ShapeRange.Line .Weight = 2 .Visible = msoTrue .ForeColor.RGB = RGB(0, 112, 192) .Transparency = 0 End With With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 11).Font.Fill .Visible = msoTrue .ForeColor.RGB = RGB(0, 112, 192) .Transparency = 0 .Solid End With End Sub
  • 请不要将代码发布到 cmets。我发布的代码适用于选定的文本框。您需要更改它以应用于您的对象。您需要确保正确处理您的对象。我不能告诉你你的文本框有什么名字或号码。我只是给了你改变颜色等所需的东西。再一次:使用宏记录器。选择您的对象。关闭宏记录器。看代码。你明白原理了吗??
【解决方案2】:

定义变量和编码后,有助于编写代码。

Sub TextBox()
    Dim myDocument As Worksheet
    Dim Shp As Shape
    Dim s As String

    Set myDocument = Worksheets(1)
    Set Shp = myDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 100, 200, 50)
    With Shp
        With .TextFrame
            s = "Test Box of how I would like it"
            .Characters.Text = s
            .Characters(1, Len(s)).Font.Color = RGB(0, 112, 193)
        End With
        .Line.Weight = 1.5
        .Line.ForeColor.RGB = RGB(0, 112, 193)
    End With

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-02
    • 2015-04-05
    • 2017-10-15
    • 2016-06-28
    • 2015-08-11
    • 2017-01-23
    • 1970-01-01
    相关资源
    最近更新 更多