Underline property of textbox not working like the others

百般思念 提交于 2021-01-29 07:21:12

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!