[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Measurestring size of text with propertional spacing

Posted on 2002-04-08
12
Medium Priority
?
2,057 Views
Last Modified: 2012-05-04
Hi,

I need to be able to calculate the size of a text string in pixels, given the font and fontsize.

It seems there is a MeasureString function in VB .net - but I need to do this in VBA in an Excel sheet.

Does anyone know the solution to this?
Is there one embedded in VBA - or do I have to go into the win32 API market ??

regards

corlin
0
Comment
Question by:corlin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +4
12 Comments
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 6924831
Ho corlin,

Get the Font Size of a column in an Excel worksheet?
0
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 6924859
If it is then try this:

On Error Resume Next
    Set appExcel = GetObject(, "Excel.Application")
    If appExcel Is Nothing Then Set appExcel = CreateObject("Excel.Application")
    On Error GoTo 0
    appExcel.Visible = True
    If Dir$(myExcel) = "" Then
        MsgBox "Unable to continue loading!", vbCritical, "Error!"
    Else
        On Error Resume Next
        Set wbExcel = appExcel.Workbooks.Open(myExcel)
    End If
   
Set WSheet = wbExcel.Worksheets(myWorkSheetName)

.
.

Msgbox WSheet.Cells(i, 1).Font.Size 'Use this

.
.
Set WSheet = Nothing
    'wbExcel.Close True
    Set wbExcel = Nothing
    'appExcel.Quit
    Set appExcel = Nothing

Is this help?
0
 

Author Comment

by:corlin
ID: 6924863
Hi ryancys,

no not the font size - but the size(width) of a text string with a font type and size.

example:
text: "iiii"
font: Arial
fontsize: 12 point
result: 16 pixels wide  <--this is what I am looking for

example:
text: "wwww"
font: Arial
fontsize: 12 point
result: 46 pixels wide  <--this is what I am looking for

I hope this shows what I am looking for

-corlin
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:jbil
ID: 6925036
Use "TextWidth"

From  help in vb6....


TextWidth Method
     

Returns the width of a text string as it would be printed in the current font of a Form, PictureBox, or Printer. Doesn't support named arguments.

Syntax

object.TextWidth(string)

The TextWidth method syntax has these parts:

Part Description
object Optional. An object expression that evaluates to an object in the Applies To list. If object is omitted, the Form with the focus is assumed to be object.
String Required. A string expression that evaluates to a string for which the text width is determined. Parentheses must surround the string expression.


Remarks

The width is expressed in terms of the ScaleMode property setting or Scale method coordinate system in effect for object. Use TextWidth to determine the amount of horizontal space required to display the text. If string contains embedded carriage returns, TextWidth returns the width of the longest line.

0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6925038
Well it is easy enough in straight VB6 but in VBA it is a bit more complicated.
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 600 total points
ID: 6925082
Here is a possible API solution as there appears to be no inbuilt VBA functions that wrap these:

Private Declare Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPoint32A" (ByVal hdc As Long, ByVal lpsz As String, ByVal cbString As Long, lpSize As Size) As Long

Private Type Size
        cx As Long
        cy As Long
End Type

Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long

Private Sub CommandButton1_Click()
    Dim ts As Size
    UserForm1.Font.Name = "Arial"
    UserForm1.Font.Size = 12
    Debug.Print GetTextExtentPoint32(GetTheDC, TextBox1.Text, Len(TextBox1.Text), ts)
    MsgBox ts.cx & vbLf & ts.cy
End Sub

Private Function GetTheDC() As Long
    Dim Ret As Long
    Ret = GetForegroundWindow()
    Ret = GetDC(Ret)
    GetTheDC = Ret
End Function
0
 

Author Comment

by:corlin
ID: 6925110
Hi TimCottee,

seems like you are on the right track - but there is missing something (or maybe I am:) ).

I have tried your code snippet (created an userform with a txtbox) - but when changing the font size results doesn't change.

Am I missing something.

-corlin
0
 
LVL 18

Expert Comment

by:mdougan
ID: 6929531
I think that you want GetTextMetrics instead.  Here is the VB Sample, but you might have to use part of Tim's sample to get the DC if it's not available as a property....


Private Declare Function GetTextMetrics Lib "gdi32" Alias "GetTextMetricsA" (ByVal hdc As Long, lpMetrics As TEXTMETRIC) As Long
Private Type TEXTMETRIC
        tmHeight As Long
        tmAscent As Long
        tmDescent As Long
        tmInternalLeading As Long
        tmExternalLeading As Long
        tmAveCharWidth As Long
        tmMaxCharWidth As Long
        tmWeight As Long
        tmOverhang As Long
        tmDigitizedAspectX As Long
        tmDigitizedAspectY As Long
        tmFirstChar As Byte
        tmLastChar As Byte
        tmDefaultChar As Byte
        tmBreakChar As Byte
        tmItalic As Byte
        tmUnderlined As Byte
        tmStruckOut As Byte
        tmPitchAndFamily As Byte
        tmCharSet As Byte
End Type

Private Sub Command1_Click()
Dim X As TEXTMETRIC
Dim RC As Long

RC = GetTextMetrics(Me.hdc, X)
Me.Print X.tmAveCharWidth
Me.Print X.tmMaxCharWidth
End Sub
0
 

Author Comment

by:corlin
ID: 6933439
Hi again,

I'm still looking for the solution - both TimCottee and mdougan's gives me the same result when changing the font size!

I'm increasing points - I really need this one solved :-)
0
 
LVL 18

Expert Comment

by:mdougan
ID: 6935066
Actually, I changed the fontsize and I got a different set of values.  I'm going to guess that you changed the fontsize of a textbox or something, but passed the hdc of the form....

I changed the Font Size of the Form when I ran my tests.

0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7643012
Hi corlin,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Split points between: TimCottee and mdougan

corlin, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 
LVL 1

Expert Comment

by:kodiakbear
ID: 7673231
Moving to the PAQ

kb
Experts Exchange Moderator

mdougan your points are here:

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20444906.html
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question