Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919

    Length of character strings in Twips

    Some interesting posts and discussions found on the net, but none appear to provide a function that will return the length of a string in twips for a given font and point size. Obviously, mono spaced fonts are an easy task and I can think of ways to write a function that employs grunt-force tables of empirically determined length, but I thought those of you out there much smarter than I might have an idea?

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,716

  3. #3
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    Dx and Dy provide the already determined size, so that's of no value with what I need. Also, the function returns boolean which is not at all what I'd like.

    E.g., with my variable lngSzTwips Dim'd as long, I'd like a function like: lngSzTwips = MyFnInTwips("my text string","Times New Roman",12). Public Function MyFnInTwips(InStr as string,FntName as string,PtSize as integer) as long. MyFnInTwips returns the length of InStr in twips.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    Dx and Dy are determined by the function, are not known inputs. Yes, function returns boolean which is used by the Sub to determine what output to present in MsgBox.

    Did you download the sample db with that article? It has a form set up for selecting font parameters. It's in German but expect not too difficult to modify. The db code corrects errors in the posted code sample, such as declaring lngSize twice.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,954
    See also my articles and video on using Wizhook.TwipsFromFont
    Autofit Text to Form Control (isladogs.co.uk)
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  6. #6
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    Yes, I've downloaded and tested Wizhook.TwipsFromFont via the sample mdb, as suggested both by Orange and Colin. What I've not found is where does one get the function?

  7. #7
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,397
    TwipsFromFont is the function, parameters may not be in the order you want and there are more that you require. So not sure what you mean by ‘where does one get the function’

  8. #8
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    I'm just getting onboard with the obscure WizHook object. I'll just have to mess around a bit with what Colin has posted on the subject and try to figure out how to access the object and make use of it. So far, all I've been able to see is that the function returns a Boolean result, I've yet to see where the aggregate twips value is obtained. It should be obvious from looking at the sample form in the mdb, so I'll continue there.

    I don't read German, but from the sample form, one can see that there's a variable buried in the Msgbox "lngBreite" that is displayed as the result. I just need to gain some understanding about the hidden objects in Access and what's the skinny with a "magic key" in order to use the object.

  9. #9
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    Using an 5-inch wide unbound text box filled to capacity with a sample text string, the WizHook sample code is off by 1.59 inches when tested with Times New Roman 12Pt font. It seemed okay with Arial, so I can only assume Times New Roman is not supported.

  10. #10
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,954
    Wizhook is a hidden function which has been built into Access since version 2000.
    You don't need to 'get it from anywhere' as its already built in

    To see it (and its 70+ members) you need to tick Show Hidden Members.
    This is explained in detail in both my various web articles, the example apps and associated videos on Wizhook

    My articles & apps build on what Thomas Moeller and others have published over the years but are all in English

    I'm not sure what sample code you are referring to but TwipsFromFont also works EXACTLY for both height and width with ALL installed fonts including Times New Roman (tested)
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  11. #11
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    I ran both these sample strings you see in the code below. WizHook reports the "s" string as having a total length in Twips at 9480, which is roughly 6.58 inches. I tested that same string TNR 12pt string in a 5 inch text box and came up with 7198 twips. Maybe I don't have the parameters correct?

    Code:
    Option Compare Database
    Option Explicit
    Private Sub testWizhook()
    
    
    'Declare variables
    Dim strCaption As String
    Dim strFontName As String
    Dim lngSize As Long
    Dim lngBreite As Long
    Dim lngHoehe As Long
    Dim fItalic As Boolean
    Dim fUnderline As Boolean
    Dim lngWidth As Long
    Dim lngHeight As Long
    
    
    'Take values
    strCaption = "Here is a sample text string measured correctly elsewhere"
    strCaption = "sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss"
    strFontName = "Times New Roman "
    lngSize = 12
    fItalic = False
    fUnderline = False
    
    
    WizHook.Key = 51488399
    If WizHook.TwipsFromFont(strFontName, lngSize, lngSize, fItalic, _
               fUnderline, 0, strCaption, 0, lngBreite, lngHoehe) = True Then
      MsgBox "The specified text has the following dimensions with the selected" & vbCrLf & _
             "font attributes:" & vbCrLf & vbCrLf & _
             "Width: " & lngBreite & vbCrLf & _
             "Height: " & lngHoehe
    Else
      MsgBox "Calculation failed", vbExclamation
    End If
    End Sub

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    5 inches X 1440 twips = 7200, close enough to 7198
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  13. #13
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    Exactly, that's why when WizHook reports 9480 I conclude something is amiss. 9480 Twips is about 6.58 inches. My 7198 number comes from a table I through together of 26 values each of which correspond to Asc "a to z".

  14. #14
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,397
    might have something to do with the space you have after the font name - probably means the font is not recognised so a default is used

    strFontName = "Times New Roman "

  15. #15
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,954
    Tested using my version of the Wizhook utility. It reports the length of your string as 7110 twips = 4.9375 inches

    Click image for larger version. 

Name:	Capture.PNG 
Views:	9 
Size:	31.4 KB 
ID:	49268

    As I posted this, I then saw the comment above by @CJ_London so I retested with a space after "Times New Roman ".
    His surmise was correct
    It now reported the width as 9480 twips as stated above

    Click image for larger version. 

Name:	Capture2.PNG 
Views:	9 
Size:	31.4 KB 
ID:	49269

    It gave the same value for other non-existent fonts such as Times Old Lomax. Also that value for Arial

    So the issue is not with Wizhook but the fact that the data you provided was incorrect.

    If you haven't yet done so, suggest you try my autofit text example app Autofit Text to Form Control (isladogs.co.uk)
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Consistent Character Length in Fields
    By Min Donner in forum Access
    Replies: 2
    Last Post: 05-10-2019, 09:29 AM
  2. dealing with zero length strings
    By markjkubicki in forum Programming
    Replies: 5
    Last Post: 10-08-2016, 07:29 PM
  3. Replies: 1
    Last Post: 09-16-2013, 04:35 PM
  4. Replies: 8
    Last Post: 02-22-2012, 11:58 AM
  5. Fixed character length of field
    By tylerg11 in forum Access
    Replies: 3
    Last Post: 09-29-2011, 11:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums