查找TextBox / Label标题是否适合控件

该场景试图调整字体大小,以获得一个漂亮的graphics安排,或试图决定在哪里打破标题/副标题。 a)在XL VBA中,有一种方法可以确定文本框上的文本或标签上的标题是否仍适合控件? b)有没有办法知道在多行控制下的文本/标题损坏了?

我给了这个rest时间,给了它足够的回头时间(这产生了远远好于“打消一个非答复ASAP,信用”的结果),并…

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),但是我会再给它一次思想上的rest…因为它比shiny地说出“不可能”更好。

我敢肯定有没有办法做到这一点在窗体工具栏上的普通Excel控件,至less因为(据我所知),他们只是图纸,而不是完整的Windows控件。

最简单的方法可能是通过一些testing对每个控件的最大文本长度进行略保守的估计,并使用这些来pipe理您的换行符。