文本框的下划线属性不能像其他文件一样工作

我只是想在工作表文本框中使用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 

代码在最后一行失败,这是非常奇怪的,因为它为前两行工作。 错误(1004)表示“不可能定义字体属性的下划线”function。

要重新创build问题,请将我的子文件添加到新的Excel文档中,并创build一个名为“txt_1”的文本框,这就是您需要运行的文本框。

如果有人有任何想法为什么失败,请帮助!

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

使用TextFrame2进行下划线

txt1.TextFrame2.TextRange.Font.UnderlineStyle = msoUnderlineSingleLine