PDA

View Full Version : [SOLVED:] Simply add three blank lines every time string shows up in doc



SBKTEX
10-29-2013, 07:27 AM
I am a trooper self-training myself Excel VBA (years of blood, sweat, tears, and satisfaction..) , but I just can't find the resources with Word VBA. I can find help on different snippets but cannot chain together rudimentary actions.. yet. This one example I/we need ASAP.

We Copy/Paste a large chunk of text from a proprietary app into Word. Within that text are instances of a unique string "abcxyz " (the post-character spaces are a secondary check). Simply, every time the string is found, go to end of that line and insert three carriage returns. Find/repeat/loop til end of document.

Seems so easy, but just can't piece it together.

Please help.. I will recycle code/actions for a number of other chores.

Thanks in advance!

Scott

gmaxey
10-29-2013, 07:39 AM
What if the string occurs twice or more in a single line?


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Dim oLineRng As Word.Range

Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "abcxyz"
While .Execute
oRng.Select
Set oLineRng = ActiveDocument.Bookmarks("\Line").Range
oLineRng.Collapse wdCollapseEnd
oRng.Collapse wdCollapseEnd
oLineRng.InsertBefore vbCr + vbCr + vbCr
Wend
End With
End Sub

SBKTEX
10-29-2013, 12:02 PM
Thank you Greg for very timely reply.. I am so confident that the string will not appear a second time in a line I will not even ask for any, even easy or practical, error catching.
I believe I should now show this as 'Solved'.
Now in the same breaths, ("Solved" , "I will recycle...").. I am wondering, and will try to solve myself, a one-off. What if I want three lines with simple "." (period) instead of blank lines? Hint (or answer!).. should I start with three single

oLineRng.InsertBefore vbCr INSERT TEXT "." 'plan of attack

OR

oLineRng.InsertBefore vbCr +INSERT TEXT "." vbCr +INSERT TEXT "." vbCr INSERT TEXT "." 'logic in a single line?

gmaxey
10-29-2013, 12:15 PM
If you are like me, you are surrounded by enough clumps of hair and bloody scalp and should not need to fret more over this:

oLineRng.InsertBefore vbCr & "." & vbCr & "." & vbCr & "."

SBKTEX
10-29-2013, 05:11 PM
thankee thankee thankee..

I got the cadence..

I tweaked

oLineRng.InsertBefore vbCr & "." & vbCr & "." & vbCr & "."

to

oLineRng.InsertBefore "." & vbCr & "." & vbCr & "." & vbCr

and it works like a champ !

We thank ya, and I'll get into your website.. //Scott