问题
I simply want to underline some text in a worksheet textbox using VBA, from a specific character to another. It should be extremely simple, and I can do it without problem with Bold and Italic.
I have the following sub
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
The code fails on the last line, which is extremely strange because it worked for the 2 previous lines. the error (1004) says something like "Impossible to define the Underline function of the Font property".
To recreate the problem, take my sub to a new Excel document and create a textbox named "txt_1", that's all you need to run it.
If anyone has any idea why it fails, please help!
回答1:
You need to define the Underline style. Taking your last line
txt1.TextFrame.Characters.Font.Underline = xlUnderlineStyleSingle
回答2:
Use TextFrame2 for underline
txt1.TextFrame2.TextRange.Font.UnderlineStyle = msoUnderlineSingleLine
来源:https://stackoverflow.com/questions/38401439/underline-property-of-textbox-not-working-like-the-others