Formatting text using Word 2003 VBA

Posted on 2009-02-13
Last Modified: 2012-05-06
Very noob question... I want to insert some text at the current cursor position, and make it bold or italic. I've messed about with selections, but it seems pretty complicated for what I want to do. Is there an easy to say "Insert some text in italics" or similar?
Question by:crescendo
    LVL 11

    Expert Comment

    You could use a range object, like this...
    Dim rng As Range
        Set rng = ActiveDocument.Content
        With rng
            'Insert some italic text'
            .Collapse wdCollapseEnd
            .InsertAfter "This is in italics" & vbCrLf
            .Font.Italic = True
            'Insert some bold text'
            .Collapse wdCollapseEnd
            .InsertAfter "This is Bold" & vbCrLf
            .Font.Bold = True
            'Insert some normal text'
            .Collapse wdCollapseEnd
            .InsertAfter "This is Normal" & vbCrLf
            .Font.Italic = False
            .Font.Bold = False
        End With
        Set rng = Nothing

    Open in new window

    LVL 9

    Author Comment

    That's much easier than I thought, thanks! However, I asked about inserting at the current cursor point, and your code puts the new text at the end of the document. Is it an easy change?
    LVL 11

    Accepted Solution

    Sorry, my mistake. Change line 2 to this:

        Set rng = Selection.Range
    LVL 9

    Author Closing Comment

    Yay! Just what I wanted! Thanks you.
    LVL 9

    Author Comment

    I have an almost identical question, where the text to be formatted is in a footnote. Could you look at it please?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    There is a feature provided by MS Word that lets you create an Table of Contents for your Word document automatically. To use this feature for other documents there are two steps involved,   1.  Prepare your document for a table of contents (he…
    When creating Microsoft Word-based forms there may be a need to have a form field repeated throughout the whole document. For instance, with a company name, you may want this information repeated automatically throughout the document rather than man…
    This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
    Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now