2008-11-02 104 views
1

该方案尝试调整字体大小以获得漂亮的图形排列,或试图决定在哪里打破标题/副标题。 a)在XL VBA中,有一种方法可以找出文本框上的文本或标签上的标题是否仍适合控件? b)是否有办法知道多行控制中文本/标题损坏的位置?查找文本框/标签标题是否适合控件

回答

2

我这给了休息,给了它足够的背的头时(产生更好的效果比“打嗝一非答案尽快,对于信贷”),并...

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),但是我会让它休息一下......因为它比闪光灯中的“不可能”更好。

+0

您首先评论(> BTW,Jeff Atwood团队...)需要去meta.stackoverflow.com。顺便说一句非常好的主意。我正在寻找几乎相同的东西,我最终在这里(最终结束了使用你的想法的变化)。谢谢! – potatopeelings 2010-12-15 09:03:00

0

我确定没有办法使用表单工具栏上的普通Excel控件执行此操作,至少因为(据我了解),它们只是绘图而不是完整的Windows控件。

最简单的方法可能是通过几次测试对每个控件的最大文本长度做一个稍保守的估计,然后使用它们来管理换行符。