该场景试图调整字体大小以获得良好的图形排列,或试图决定在哪里打破标题/副标题。
a) 在 XL VBA 中,有没有办法找出文本框上的文本或标签上的标题是否仍然适合控件?
b) 有没有办法知道多行控件上的文本/标题在哪里被破坏?
最佳答案
我休息了一下,给了它足够的回想时间(这比“尽快打嗝,以获得学分”产生更好的结果),并且......
Function TextWidth(aText As String, Optional aFont As NewFont) As Single
Dim theFont As New NewFont
Dim notSeenTBox As Control
On Error Resume Next 'trap for aFont=Nothing
theFont = aFont 'try assign
If Err.Number Then 'can't use aFont because it's not instantiated/set
theFont.Name = "Tahoma"
theFont.Size = 8
theFont.Bold = False
theFont.Italic = False
End If
On Error GoTo ErrHandler
'make a TextBox, fiddle with autosize et al, retrive control width
Set notSeenTBox = UserForms(0).Controls.Add("Forms.TextBox.1", "notSeen1", False)
notSeenTBox.MultiLine = False
notSeenTBox.AutoSize = True 'the trick
notSeenTBox.Font.Name = theFont.Name
notSeenTBox.SpecialEffect = 0
notSeenTBox.Width = 0 ' otherwise we get an offset (a ""feature"" from MS)
notSeenTBox.Text = aText
TextWidth = notSeenTBox.Width
'done with it, to scrap I say
UserForms(0).Controls.Remove ("notSeen1")
Exit Function
ErrHandler:
TextWidth = -1
MsgBox "TextWidth failed: " + Err.Description
End Function
我觉得我正在/接近答案 b),但我会让它再休息一下……因为它比一闪而过地说“不可能”更好。
关于vba - 查找文本框/标签标题是否适合控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/256823/