[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 553
  • Last Modified:

Formatting text using Word 2003 VBA

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?
0
crescendo
Asked:
crescendo
  • 3
  • 2
1 Solution
 
Antagony1960Commented:
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

0
 
crescendoAuthor Commented:
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?
0
 
Antagony1960Commented:
Sorry, my mistake. Change line 2 to this:

    Set rng = Selection.Range
0
 
crescendoAuthor Commented:
Yay! Just what I wanted! Thanks you.
0
 
crescendoAuthor Commented:
I have an almost identical question, where the text to be formatted is in a footnote. Could you look at it please?
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_24140869.html
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now