【问题标题】:Underline property of textbox not working like the others文本框的下划线属性不像其他的那样工作
【发布时间】:2016-07-15 16:54:14
【问题描述】:

我只是想使用 VBA 为工作表文本框中的某些文本添加下划线,从特定字符到另一个字符。它应该非常简单,我可以毫无问题地使用粗体和斜体。

我有以下子

Sub ew()
    Dim txt1 As Shape
    Set txt1 = Sheet1.Shapes("txt_1")
    txt1.TextFrame.Characters.Text = "Bold and Underline this"

    txt1.TextFrame.Characters.Font.Bold = True
    txt1.TextFrame.Characters.Font.Italic = True
    txt1.TextFrame.Characters.Font.Underline = True
End Sub

代码在最后一行失败,这非常奇怪,因为它适用于前 2 行。错误 (1004) 表示“无法定义字体属性的下划线功能”。

要重现问题,请将我的 sub 转到一个新的 Excel 文档并创建一个名为“txt_1”的文本框,这就是您运行它所需的全部内容。

如果有人知道它失败的原因,请帮忙!

【问题讨论】:

标签: vba excel


【解决方案1】:

您需要定义下划线样式。最后一行 txt1.TextFrame.Characters.Font.Underline = xlUnderlineStyleSingle

【讨论】:

【解决方案2】:

使用TextFrame2 下划线

txt1.TextFrame2.TextRange.Font.UnderlineStyle = msoUnderlineSingleLine

【讨论】:

  • TextFrame2 为您提供更多下划线样式,如果您想获得真正的花哨。 :)
猜你喜欢
  • 2018-01-12
  • 1970-01-01
  • 1970-01-01
  • 2014-02-24
  • 2011-05-01
  • 2011-08-19
  • 2012-04-15
  • 2016-10-19
  • 1970-01-01
相关资源
最近更新 更多