Results 1 to 7 of 7

Thread: [Word] Trying to automatically change the font size of numbers in a textbox

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2021
    Posts
    2

    [Word] Trying to automatically change the font size of numbers in a textbox

    Hi everyone,

    I have very little experience with VB in general, but I'm trying to change the font size of any number found in a given textbox. When I run the module (or macro? I'm not sure if there is a difference), I want it to find any number in a given text and increase that number by 3 font sizes.

    Example:

    TWO-ACADEMY AWARD® WINNER DUSTIN HOFFMAN (BEST ACTOR —RAIN MAN, 1998; KRAMER VS. KRAMER, 1979) STARS IN THE ACTION-PACKED GANGSTER EPIC DETAILING THE RISE AND FALL OF NOTORIOUS MOBSTER DUTCH SCHULTZ AS SEEN THROUGH THE EYES OF YOUNG PROTÉGÉ, BILLY BATHGATE.

    Output:

    TWO-ACADEMY AWARD® WINNER DUSTIN HOFFMAN (BEST ACTOR —RAIN MAN, 1998; KRAMER VS. KRAMER, 1979) STARS IN THE ACTION-PACKED GANGSTER EPIC DETAILING THE RISE AND FALL OF NOTORIOUS MOBSTER DUTCH SCHULTZ AS SEEN THROUGH THE EYES OF YOUNG PROTÉGÉ, BILLY BATHGATE.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Word] Trying to automatically change the font size of numbers in a textbox

    maybe this is just a website issue, but i really can not see the difference in output

    you can try like
    Code:
    Set tb = ThisDocument.Shapes(1)
    With tb
        For Each ch In .TextFrame.TextRange.Characters
            If IsNumeric(ch) Then ch.Font.Size = ch.Font.Size + 3
        Next
    End With
    where tb is your specific textbox
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: [Word] Trying to automatically change the font size of numbers in a textbox

    Pete, there is a difference in #1 (but i had to look twice, too)

    The OP states "textbox"
    The question now is: Is that in a UserForm or as you assumed a "shape" in the document itself?
    Because if it's a UserForm i wasn't aware that you can set the FontSize for partial String in a regular Textbox
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Word] Trying to automatically change the font size of numbers in a textbox

    (but i had to look twice, too)
    it certainly was more evident in the sample i tested

    i was only guessing it was even in word, nevermind what type of container, but no mention anywhere about a userform
    maybe s/he will come back and clarify
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2021
    Posts
    2

    Re: [Word] Trying to automatically change the font size of numbers in a textbox

    Hi everyone!

    Thank you for the help. I'm sorry about the confusion with the word textbox, it's just what I thought we called any given text in a word document that we intend to use a macro on. I have very little experience with programming, so apparently, I used the wrong terminology.

    What I need is something that, when I activate it, parses through any given text, finds the numbers and makes them 3 sizes bigger.

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Word] Trying to automatically change the font size of numbers in a textbox

    parses through any given text
    the above code would probably work for any given text, but how you specify the text to work with will change, but you have not given any clue as to the range of text you want to work with

    part of documents are specified as ranges, but to set the range object you need to know how to specify the starting and finishing points in the text to be limits of the range object
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: [Word] Trying to automatically change the font size of numbers in a textbox

    Independant of the "Textrange": I'd probably throw a RegEx against it.
    The resulting Array can be used to sequentially walk Through the "Textrange" and change the Fontsize as Pete demonstrated in his Post #2

    Nota Bene: AFAIK, a RegEx only returns the matches, not the position, so you also have to take care, if a match occurs multiple times
    (No idea how RegEx handles duplicates)
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

Posting Permissions

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



Click Here to Expand Forum to Full Width