newbie question

J

John Keith

I've done a fair amount of VBA work with Excel but now I have a need
to do a task that is text oriented and Word is the first application
that seems appropriate to try. I've played with the macro recorder
some and have a few fundamental items working but my initial
impression is there is some substantial differences between word
commands and excel commands (I am not a frequent word user) and it
looks like even with some excel experience there is a learning curve
ahead for me.

With that background could some one provide me with a tip on how to
accomplish the following task:

after opening a text file (that has no formatting):

search for every occurrence of "source string"

add a line of text below the line containing the "source string"

with "add this text string"

I'd be interested in recommendations for any good online tutorials for
VBA for Word as I haven't found any yet (results of google searches do
not seem to be as rich for Word as they are for Excel).


John Keith
(e-mail address removed)
 
D

Doug Robbins - Word MVP

Use:

Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="Source String", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False) = True
Selection.Paragraphs(1).Range.InsertAfter "Add this text String" &
vbCr
Selection.Collapse wdCollapseEnd
Selection.MoveRight wdCharacter, 1
Loop
End With

See the article "Getting To Grips With VBA Basics In 15 Minutes" at:

http://www.word.mvps.org/FAQs/MacrosVBA/VBABasicsIn15Mins.htm

and other pages on that site.


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
J

John Keith

[code deleted]

Doug,

Thanks, your code worked great. I'm going to try a couple more baby
steps.

When I added this code to the code I had started with and then clicked
on close the file I wasn't asked if I wanted to save the changes, Word
just closed (and the changes were saved.) This seems different form my
experience with Excel.


John Keith
(e-mail address removed)
 
J

John Keith

See the article "Getting To Grips With VBA Basics In 15 Minutes" at:

http://www.word.mvps.org/FAQs/MacrosVBA/VBABasicsIn15Mins.htm

and other pages on that site.

Doug,

Thank you for pointing me to this site, it looks like a good place to
study not only for macros but Word in general.

If I might ask a question on the code you provided. Suppose after
finding the "source string" I want to add a line of text not
imediately below but rather "n" lines of text below how would the code
change?



John Keith
(e-mail address removed)
 
D

Doug Robbins - Word MVP

Assuming that each of these lines are paragraphs (that is they end in a hard
return) then the following
would insert the text after the fourth paragraph

Dim selrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="Source String", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False) = True
Set selrange = Selection.Paragraphs(1).Range
selrange.MoveEnd wdParagraph, 4
selrange.InsertAfter "Add this text String" & vbCr
Selection.Collapse wdCollapseEnd
Selection.MoveRight wdCharacter, 1
Loop
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
J

John Keith

Assuming that each of these lines are paragraphs (that is they end in a hard
return) then the following
would insert the text after the fourth paragraph

Doug,

Perfect! Thank you.


John Keith
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top