Solved

Inserting text into a word document using a macro

Posted on 2015-01-22
2
87 Views
Last Modified: 2015-01-23
I am trying to write a macro to insert a word at the beginning of each paragraph based on a selection in a Microsoft Word document.  For example, "1. Some text here" would change to "WORD 1. Some text here" after running the macro.  So far I have this, but I only want the Macro to run with a selection, not the whole document.

Sub addNotes()

    iParCount = ActiveDocument.Paragraphs.Count
    For j = 1 To iParCount
        sMyPar = "Note " + ActiveDocument.Paragraphs(j).Range.Text
        ActiveDocument.Paragraphs(j).Range.Text = sMyPar
    Next j

End Sub

Thanks.
0
Comment
Question by:rcimasi
2 Comments
 
LVL 50

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40565893
Hi,

pls try

Sub addNotes()
    Set myRange = Selection.Range

    For Idx = 1 To myRange.Paragraphs.Count
        myRange.Paragraphs(Idx).Range.Text = "Note " + myRange.Paragraphs(Idx).Range.Text
    Next

End Sub

Open in new window

Regards
0
 

Author Comment

by:rcimasi
ID: 40566506
That worked like a charm, thanks.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an explanation of a simple data model to help parse a JSON feed
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn how to maximize accessibility options in an Excel workbook for users with accessibility issues.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

820 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