Solved

Inserting text into a word document using a macro

Posted on 2015-01-22
2
84 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 49

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

24 Experts available now in Live!

Get 1:1 Help Now