Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7696485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:47:23+00:00 2026-05-31T21:47:23+00:00

How would you calculate the number of pixels for a String (in an arbitrary

  • 0

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-questions/19267-width-specific-text-pixels.html
  • http://www.ozgrid.com/forum/showthread.php?t=94339
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:47:25+00:00Added an answer on May 31, 2026 at 9:47 pm

    Write a new module class and put the following code in it.

    'Option Explicit
    
    'API Declares
    
    Private Declare Function CreateDC Lib "gdi32.dll" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, lpInitData As Long) As Long
    Private Declare Function CreateCompatibleBitmap Lib "gdi32.dll" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
    Private Declare Function CreateFontIndirect Lib "gdi32.dll" Alias "CreateFontIndirectA" (lpLogFont As LOGFONT) As Long
    Private Declare Function SelectObject Lib "gdi32.dll" (ByVal hdc As Long, ByVal hObject As Long) As Long
    Private Declare Function DeleteObject Lib "gdi32.dll" (ByVal hObject As Long) As Long
    Private Declare Function GetTextExtentPoint32 Lib "gdi32.dll" Alias "GetTextExtentPoint32A" (ByVal hdc As Long, ByVal lpsz As String, ByVal cbString As Long, lpSize As SIZE) As Long
    Private Declare Function MulDiv Lib "kernel32.dll" (ByVal nNumber As Long, ByVal nNumerator As Long, ByVal nDenominator As Long) As Long
    Private Declare Function GetDC Lib "user32.dll" (ByVal hwnd As Long) As Long
    Private Declare Function GetDeviceCaps Lib "gdi32.dll" (ByVal hdc As Long, ByVal nIndex As Long) As Long
    Private Declare Function DeleteDC Lib "gdi32.dll" (ByVal hdc As Long) As Long
    
    Private Const LOGPIXELSY As Long = 90
    
    Private Type LOGFONT
        lfHeight As Long
        lfWidth As Long
        lfEscapement As Long
        lfOrientation As Long
        lfWeight As Long
        lfItalic As Byte
        lfUnderline As Byte
        lfStrikeOut As Byte
        lfCharSet As Byte
        lfOutPrecision As Byte
        lfClipPrecision As Byte
        lfQuality As Byte
        lfPitchAndFamily As Byte
        lfFaceName As String * 32
    End Type
    
    Private Type SIZE
        cx As Long
        cy As Long
    End Type
    Public Function getLabelPixel(label As String) As Integer
    
      Dim font As New StdFont
      Dim sz As SIZE
      font.Name = "Arial Narrow"
      font.SIZE = 9.5
    
      sz = GetLabelSize(label, font)
      getLabelPixel = sz.cx
    
    End Function
    
    Private Function GetLabelSize(text As String, font As StdFont) As SIZE
        Dim tempDC As Long
        Dim tempBMP As Long
        Dim f As Long
        Dim lf As LOGFONT
        Dim textSize As SIZE
    
        ' Create a device context and a bitmap that can be used to store a
        ' temporary font object
        tempDC = CreateDC("DISPLAY", vbNullString, vbNullString, ByVal 0)
        tempBMP = CreateCompatibleBitmap(tempDC, 1, 1)
    
        ' Assign the bitmap to the device context
        DeleteObject SelectObject(tempDC, tempBMP)
    
        ' Set up the LOGFONT structure and create the font
        lf.lfFaceName = font.Name & Chr$(0)
        lf.lfHeight = -MulDiv(font.SIZE, GetDeviceCaps(GetDC(0), 90), 72) 'LOGPIXELSY
        lf.lfItalic = font.Italic
        lf.lfStrikeOut = font.Strikethrough
        lf.lfUnderline = font.Underline
        If font.Bold Then lf.lfWeight = 800 Else lf.lfWeight = 400
        f = CreateFontIndirect(lf)
    
        ' Assign the font to the device context
        DeleteObject SelectObject(tempDC, f)
    
        ' Measure the text, and return it into the textSize SIZE structure
        GetTextExtentPoint32 tempDC, text, Len(text), textSize
    
        ' Clean up (very important to avoid memory leaks!)
        DeleteObject f
        DeleteObject tempBMP
        DeleteDC tempDC
      ' Return the measurements
        GetLabelSize = textSize
    
    End Function
    

    Call the getLabelPixel function with parameter(string whose width has to be calculated).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would you calculate the total number of possibilities that binary can have in
I would like to calculate the number of periods that have elapsed since the
I was given this Python code that would calculate an MD5 value for any
I would like to calculate total order amount in the previous month. I got
I would like to calculate my total order amount in the previous week. I
I would like to calculate the similarity between users, which is reciprocal. similarity[:user1][:user2] ==
I would like to calculate a timestamp based on the input date. I would
I would like to calculate a density function of a distribution whose characteristics function
Lets say I would like to calculate percents and do: double n = ...;
I have a matrix mat and would like to calculate the mean of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.