PDA

View Full Version : Solved: Inserting text into each line of a selection



runofthemill
01-16-2013, 03:15 PM
Hello,

I'm trying to figure out how to code a macro to insert specific text at the beginning of each line of a user-defined selection. In essence, the user selects part of a list (defined as one item per line) and clicks a button to run the macro, which tags each item in that list.

The problem I am having is that I cannot figure out how to define the length of a loop if it's based on the length of a user selection. Each iteration of the loop should do this:

Selection.HomeKey Unit:=wdLine
Selection.TypeText Text:="[Repeat SKU] "
Selection.MoveDown Unit:=wdLine, Count:=1

Should I be passing the selection into a range object? I'm a bit of a VBA novice here, and any and all help is very much appreciated. Thanks!

gmaxey
01-16-2013, 09:08 PM
Do you mean one item per paragraph?

Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oPar As Word.Paragraph
For Each oPar In Selection.Paragraphs
oPar.Range.InsertBefore "[Repeate SKU]"
Next oPar

End Sub

runofthemill
01-16-2013, 09:17 PM
Perfect! I didn't think to view each line as it's own paragraph, but that is exactly what it is. I've been wracking my brain for a few days over this - your help is much appreciated!

gmaxey
01-16-2013, 09:37 PM
If you are anything like me and you stick with VBA then your brain is in for frequenct wrackings over many, many days.

Glad I could help.