• Apply font attributes to string (VB/Word 97)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Apply font attributes to string (VB/Word 97)

    • This topic has 6 replies, 3 voices, and was last updated 23 years ago.
    Author
    Topic
    #370393

    This novice writes:

    I have the following code and need to apply bold, small caps to “frmDeed.TextBox4.Text” only….

    ActiveDocument.Variables.Add Name:=”AddSellerAddress”, Value:=” and ” & _
    frmDeed.TextBox4.Text & “, residing at ” & frmDeed.TextBox5.Text & “, ” & frmDeed.TextBox6.Text

    I’ve tried a few things but can’t seem to get it. Thank you for your advice.

    Viewing 0 reply threads
    Author
    Replies
    • #585933

      A document variable is (in Word 97 at least) just a string, with no formatting.
      If you want to store formatted text, consider the use of AutoText fragments.

      • #585934

        But I don’t know what will be contained in TextBox4…if anything. I’m using autotext throught the process, but how would I use autotext in this instance?

        • #585942

          I haven’t really thought this through. Usually, you use predefined AutoText entries. But you can also create and modify them in code.
          In the code you tried, you store some text in a document variable using code. Instead of that, you could use code to store the text (including the contents of TextBox4 and the desired formatting) in an AutoText entry.
          Perhaps somebody will come up with better ideas if you explain what you want to do with this.

          • #585973

            Thank you Hans. I’m just inserting some variable information in the middle of a paragraph, but the Seller’s name should be bold, small caps, and the rest normal text. There may or may not be a second Seller, but if there is, I wanted it to be formatted with those font attributes.

            • #586151

              Best might be to have some placeholder text there, such as *JoanneTextHere* that is styled as you want the inserted text to be styled. If you then select the placeholder and use Selection.TypeText to replace it with the contents of your text string, it should take on the font attributes of what it replaces. Alternatively, you could use Replace, if the placeholder is guaranteed to be unique.

            • #586216

              Thank you! That got me on the right track. I just had to break out the original string a little more…. here’s the code that worked for me:

              Selection.GoTo what:=wdGoToBookmark, Name:=”Seller2″
              Selection.TypeText “, and ”
              With Selection.Font
              .Name = “Times New Roman Bold”
              .Bold = True
              .SmallCaps = True
              End With
              Selection.TypeText frmDeed.TextBox4.Text
              With Selection.Font
              .Name = “Times New Roman”
              .Bold = False
              .SmallCaps = False
              End With
              ActiveDocument.Variables.Add Name:=”AddSellerAddress”, Value:=”, ” & _
              “residing at ” & frmDeed.TextBox5.Text & “, ” & frmDeed.TextBox6.Text

              I very much appreciate your help. Sometimes I think I make things more difficult than they have to be….

    Viewing 0 reply threads
    Reply To: Apply font attributes to string (VB/Word 97)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: