更改文本框的字体颜色 [英] Change font colour of a textbox

查看:79
本文介绍了更改文本框的字体颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打开一个Excel文件,转到文件的第一页,然后将textbox1的文本颜色更改为红色.

I want to open an Excel file, go to the first sheet in the file, and change the text colour of textbox1 to red.

到目前为止,我唯一设法做到的就是通过录制宏.

The only way I have managed to do it so far is via recording the macro.

它给了我

Workbooks.Open (fPath & sName)

            Sheets(1).Select

  ActiveSheet.Shapes.Range(Array("TextBox1")).Select

  With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 262).Font.Fill
    .Visible = msoTrue
    .ForeColor.RGB = RGB(255, 0, 0)
    .Transparency = 0
    .Solid
    End With

没关系;但是文本的长度是可变的,因此如果它少于上面的262个字符,则会出现代码错误.

That's fine; however the length of the text is variable so I get an error with the code if it is less than the 262 characters above.

我试图介绍

CharCount = Len(textbox1.Text)

但是我收到错误424对象必需

However I get error 424 Object required

我最初尝试过

Sheets(1).Select
ActiveSheet.TextBox1.ForeColor = RGB(255, 0, 0)

但出现错误438对象不支持此属性或方法.

but got error 438 Object doesn't support this property or method.

推荐答案

如果您想更改整个文本框的字体颜色(即不仅是某些字符),请跳过 Characters 方法.另外,您不应该依赖.选择 ActiveSheet 等.而是设置适当的引用.

If you want to change the font colour of the entire textbox (i.e. not just certain characters) then skip the Characters method. Also you shouldn't rely on .Select, ActiveSheet and the likes. Set proper references instead.

这有效:

Dim wb As Workbook
Dim ws As Worksheet
Dim s As Shape

Set wb = Workbooks.Open(fPath & sName)
Set ws = wb.Sheets(1)
Set s = ws.Shapes("TextBox 1")

s.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0)

这篇关于更改文本框的字体颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆