Solved

Inserting text into a word document using a macro

Posted on 2015-01-22
2
80 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 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
Comment Utility
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
Comment Utility
That worked like a charm, thanks.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

744 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

18 Experts available now in Live!

Get 1:1 Help Now