David G
David G

Reputation: 2355

Underline property of textbox not working like the others

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!

Upvotes: 0

Views: 2312

Answers (2)

Darrell H
Darrell H

Reputation: 1886

You need to define the Underline style. Taking your last line txt1.TextFrame.Characters.Font.Underline = xlUnderlineStyleSingle

Upvotes: 5

cyboashu
cyboashu

Reputation: 10443

Use TextFrame2 for underline

txt1.TextFrame2.TextRange.Font.UnderlineStyle = msoUnderlineSingleLine

Upvotes: 3

Related Questions