Results 1 to 11 of 11
  1. #1
    Star Lounger
    Join Date
    May 2005
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Resizing Texbox to Accommodate More Text (VBA - Word XP)

    Does anyone have a neat, clean way to have a textbox resize to fit the amount of text being pulled into it from a database? I need this textbox to fluctuate in size depending on the amount of text, usually not more than 250 words. Thanks. Penni

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Resizing Texbox to Accommodate More Text (VBA - Word XP)

    You haven't told us what kind of text box you mean, but I don't think there is a "neat, clean" way to do what you want, in particular if the text box uses a proportional font such as Times New Roman or Arial. It's best to make the text box large enough for the longest text you expect.

  3. #3
    Star Lounger
    Join Date
    May 2005
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Resizing Texbox to Accommodate More Text (VBA - Word XP)

    Thanks, Hans. I was afraid of that. I just wanted my plain, old, ordinary textbox to grow with the text, say from a height of 18 to 36. I was hoping to grow it enough so that the form user could see most all of the text of the textbox on the userform. Thanks, again. Penni

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Resizing Texbox to Accommodate More Text (VBA - Word XP)

    You could try to use trial-and-error to find a rough rule-of-thumb relationship between the length (in characters) of the text and the height of the text box, but it will just be an approximation - the string "wwww" takes up a lot more space than "iiii", although both contain 4 characters.

  5. #5
    Star Lounger
    Join Date
    May 2005
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Resizing Texbox to Accommodate More Text (VBA - Word XP)

    Ultimately, that's what I am going to do. This form will have a textbox with a height that is twice as much as needed for most text and not quite long enough for the longest text. However, the user will be able to see that there is more text sitting there. Crisis solved for this day, anyway. Thanks again! Penni

  6. #6
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Resizing Texbox to Accommodate More Text (VBA - Word XP)

    Maybe the attached sample will give you an idea. Key is the line .Width = 200 which should always be executed before putting text in.

  7. #7
    Star Lounger
    Join Date
    May 2005
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Resizing Texbox to Accommodate More Text (VBA - Word XP)

    Thanks for the information. I did, in fact, have Autosize and Multiline equaling true, as well as WordWrap. Are you saying that there is a way to count the text and then change the width of the box? Thanks, again. Penni

  8. #8
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Resizing Texbox to Accommodate More Text (VBA - Word XP)

    No need to calculate the length of the text. See example. The TextBox has a fixed width and a resizing height.

  9. #9
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Resizing Texbox to Accommodate More Text (VBA - Word XP)

    Here's a solution from VBForums - Measure Character Width (regardless of controls and forms). I'll copy it here because the link (for me) seems a bit flakey.

    "I've written a class for you that you (and others) might find useful. It only contains a Font property and a TextWidth and a TextHeight method. TextWidth|Height will always return the width/height of the supplied text in pixels. If a multilined text is supplied to the TextWidth method, the return value will be the length of the longest line. TextHeight will return the height needed for all lines to be printed.

    It's very easy to use:
    visual basic code:
    --------------------------------------------------------------------------------
    Dim o As CTextSize
    Set o = New CTextSize

    With o.Font
    .Name = "Times New Roman"
    .Size = 12
    .Bold = True
    End With
    MsgBox o.TextWidth("Hello World!")
    --------------------------------------------------------------------------------

    If no Font has been supplied the class will use MS Sans Serif, 8pt, with normal font style (not bold or italic)." ... Joacim Andersson

    Alan

  10. #10
    Star Lounger
    Join Date
    May 2005
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Resizing Texbox to Accommodate More Text (VBA - Word XP)

    Sheesh. Didn't realize that you had placed code behind the Word doc attachment. Duh. Thanks!! Now I get it. Penni

  11. #11
    Star Lounger
    Join Date
    May 2005
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Resizing Texbox to Accommodate More Text (VBA - Word XP)

    Thanks, Allan. I'll give it a try. Penni

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •