以编程方式访问VBA设置字体/大小不起作用 [英] Access VBA programmatically setting font/size not working

查看:373
本文介绍了以编程方式访问VBA设置字体/大小不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,应该将文本框的字体和大小更改为Tahoma 8pt。按钮事件是:

pre $私人小组btnSetFont_Click()
MsgBox(设置库存描述到Tahoma 8pt)
Me.InventoryDe​​scription.FontSize = 8
Me.InventoryDe​​scription.FontName =Tahoma
End Sub

不幸的是,文字并没有改变。我正在测试它,首先手动编辑字体和大小,然后按下我的按钮。

但是,如果我执行以下操作,



pre $私人小组btnSetFont_Click()
MsgBox(设置库存描述到Tahoma 8pt)
Me.InventoryDe​​scription.Value =hello
Me.InventoryDe​​scription.FontSize = 24
Me.InventoryDe​​scription.FontName =Times
End Sub

文本当然会变成hello,但字体和大小确实会改变。 (我使用Times 24pt,因为文本框的默认值是Tahoma 8pt,我想确保它不仅仅是恢复到默认值)这使我认为文本框需要有重点进行更改。所以,我试过:
$ b $ pre $私人小组btnSetFont_Click()
MsgBox(设置库存描述到Tahoma 8pt)
Me.InventoryDe​​scription.SetFocus
Me.InventoryDe​​scription.FontSize = 24
Me.InventoryDe​​scription.FontName =Times
End Sub

但是,不行。

Soooo,我做错了什么?






我发现问题的一个方面。文本框.TextFormat设置为Rich Text。如果我将其更改为纯文本,则按钮效果起作用。但是,设置为Rich Text的原因是允许使用斜体。所以,我试着先把它设置成纯文本,然后改变字体/大小,但是这也不起作用。我有同样的需求:我希望用户能够用粗体,斜体和下划线字符格式化文本,但我不希望允许字体名称更改或字体大小更改。复制/粘贴操作经常导入我的文本框中的格式化文本,这需要清理。

我找到的解决方案在下面的函数中。
这个函数应该被一个事件过程调用(即更新后,或者点击)。



<$ p $公共函数CleanRichText(strTEXT,strFont,nSize)
'*************************** **************************
'

对于i = 1至9
strTEXT =替换(strTEXT,size =& i,size =& nSize)
下一个我

strTEXT =替换(strTEXT,font face,font_face)
strTEXT =替换(strTEXT,font& Chr(13)& Chr(10)&face,font_face)

在InStr(1,strTEXT ,font_face =& Chr(34))> 0
iCut1 = InStr(1,strTEXT,font_face =& Chr(34))
iCut2 = InStr(iCut1 + 12,strTEXT,Chr(34))
strLeft = (strTEXT,iCut1-1)& font_face = Face
strRight = Right(strTEXT,Len(strTEXT) - iCut2)
strTEXT = strLeft& strRight
Loop

Do InStr(1,strTEXT,font_face =)> 0
iCut1 = InStr(1,strTEXT,font_face =)
iCut2 = InStr(iCut1 + 12,strTEXT,Chr(32))
strLeft = Left(strTEXT,iCut1 - 1 )& font face =& strFont& Chr(32)
strRight = Right(strTEXT,Len(strTEXT) - iCut2)
strTEXT = strLeft& strRight
Loop
CleanRichText = strTEXT

End Function



< hr>

  Private Sub Cause_AfterUpdate()

Me.Cause = CleanRichText(Me.Cause,Me.Cause.FontName ,2)
End Sub


I have a button that is supposed to change the font face and size of a textbox to Tahoma 8pt. The button event is:

Private Sub btnSetFont_Click()
    MsgBox ("Setting Inventory Description to Tahoma 8pt")
    Me.InventoryDescription.FontSize = 8
    Me.InventoryDescription.FontName = "Tahoma"
End Sub

Unfortunately, the text does not change. I'm testing it by first editing the font and size by hand, and then pressing my button.

However, if I do the following,

Private Sub btnSetFont_Click()
    MsgBox ("Setting Inventory Description to Tahoma 8pt")
    Me.InventoryDescription.Value = "hello"
    Me.InventoryDescription.FontSize = 24
    Me.InventoryDescription.FontName = "Times"
End Sub

The text changes to "hello" of course, but the font and size do indeed change. (I used Times 24pt because the default for the textbox is Tahoma 8pt and I wanted to make sure it wasn't just reverting to the default) This made me think that the textbox needs to have the focus to make the changes. So, I tried:

Private Sub btnSetFont_Click()
    MsgBox ("Setting Inventory Description to Tahoma 8pt")
    Me.InventoryDescription.SetFocus
    Me.InventoryDescription.FontSize = 24
    Me.InventoryDescription.FontName = "Times"
End Sub

But, no go.

Soooo, what am I doing wrong?


I found one aspect to the problem. The text box .TextFormat is set to Rich Text. If I change it to Plain Text, then the button effect works. However, the reason it is set to Rich Text is to allow italics. So, I tried first setting it to plain text, and then changing the font/size, but that didn't work either.

解决方案

I have the same need: I want the users to be able to format the text with boldface, italics and underlined characters, but I don't want to allow font name changes, or font size changes. And copy/paste actions often import formatted text in my textBox, which needs to be "cleaned".

The solution I found is in the Function below. This function should be called by an Event Procedure (i.e. After Update, or On Click).


Public Function CleanRichText(strTEXT, strFont, nSize)
'*****************************************************
' 

    For i = 1 To 9
        strTEXT = Replace(strTEXT, "size=" & i, "size=" & nSize)
    Next i

    strTEXT = Replace(strTEXT, "font face", "font_face")
    strTEXT = Replace(strTEXT, "font" & Chr(13) & Chr(10) & "face", "font_face")

    Do While InStr(1, strTEXT, "font_face=" & Chr(34)) > 0
        iCut1 = InStr(1, strTEXT, "font_face=" & Chr(34))
        iCut2 = InStr(iCut1 + 12, strTEXT, Chr(34))
        strLeft = Left(strTEXT, iCut1 - 1) & "font_face=Face"
        strRight = Right(strTEXT, Len(strTEXT) - iCut2)
        strTEXT = strLeft & strRight
    Loop

    Do While InStr(1, strTEXT, "font_face=") > 0
        iCut1 = InStr(1, strTEXT, "font_face=")
        iCut2 = InStr(iCut1 + 12, strTEXT, Chr(32))
        strLeft = Left(strTEXT, iCut1 - 1) & "font face=" & strFont & Chr(32)
        strRight = Right(strTEXT, Len(strTEXT) - iCut2)
        strTEXT = strLeft & strRight
    Loop
    CleanRichText = strTEXT

End Function


Private Sub Cause_AfterUpdate()

    Me.Cause = CleanRichText(Me.Cause, Me.Cause.FontName, 2)
End Sub

这篇关于以编程方式访问VBA设置字体/大小不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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