vb macro string width in pixel

前端 未结 9 1526
感动是毒
感动是毒 2020-12-09 06:25

How would you calculate the number of pixels for a String (in an arbitrary font), using an Excel VBA macro?

Related:

  • http://www.mrexcel.com/forum/excel
9条回答
  •  情深已故
    2020-12-09 06:53

    If you are using a UserForm, a much less technically solution would be to add a label to the form with the same font style and size as the text to be evaluated. Set AutoSize to True, Caption to 'blank', Visible to False, Width to 0, and wordWrap to False.

    enter image description here

    This hidden label will become of measurement tool of sorts for text using the Function below:

    Public Function TextLength(sString As String) As Long
        UserForm.TextMeasure.Caption = sString
        TextLength = UserForm.TextMeasure.Width
    End Function
    

提交回复
热议问题