Using Find Then Inserting Text

B

Bad at VBA

I'm not quite understanding how to control where text will be inserted
when using a macro.

I'd like to insert text after a particular heading in a bunch of
documents. I know that the heading will use style Heading 1, and I
know the section will be called "Test".

So what I was thinking was that I'd search for the style and the text,
and then use the Selection.InsertParagraphAfter to insert my text.

And while this works in that it finds the text, I'm not sure how to
place the insertion point after the found text. It always inserts at
the top of the document.

Is what I'm trying to do possible, or am I just going after it the
wrong way?

The documents are structured similar to:

INTRODUCTION

Some text

DESCRIPTION

Some text

TEST

Some text

What I want to do is insert new text after TEST and before "Some
text".

Any help would be greatly appreciated!
 
G

Greg Maxey

I suspect that you want the inserted text to be in a following
paragraph with the appropriate style applied. I am not sure if this
is the best way to do this, but it seems to work:

Sub Scratchmacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "Test"
.Style = "Heading 1"
While .Execute
oRng.Collapse wdCollapseEnd
oRng.InsertAfter vbCr
oRng.Move wdParagraph, 1
oRng.Style = ActiveDocument.Styles("Heading 1").NextParagraphStyle
oRng.InsertAfter "your text"
Wend
End With
End Sub
 
B

Bad at VBA

Thank you, thank you, thank you. It works great!

I suspect that you want the inserted text to be in a following
paragraph with the appropriate style applied. I am not sure if this
is the best way to do this, but it seems to work:

Sub Scratchmacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "Test"
.Style = "Heading 1"
While .Execute
oRng.Collapse wdCollapseEnd
oRng.InsertAfter vbCr
oRng.Move wdParagraph, 1
oRng.Style = ActiveDocument.Styles("Heading 1").NextParagraphStyle
oRng.InsertAfter "your text"
Wend
End With
End Sub
 
H

Helmut Weber

Hi,
Any help would be greatly appreciated!

if so, then have a look at this one:

Sub Test45567()
ActiveDocument.Range(0, 0).Select
With Selection.Find
.Text = "Test"
.MatchCase = True
.Style = "Heading 1"
If .Execute Then ' just in case
Selection.Paragraphs(1).Range.Select
Selection.InsertAfter Chr(13)
Selection.Paragraphs(2).Range.Text = _
"This is the added text" & Chr(13)
End If
End With
End Sub

Explanation:
Move the cursor the the start of the doc.
Search for "Test"
in a paragraph of style "Heading 1".
If found (.execute), then
select the whole paragraph.
Otherwise only "Test" would be selected.
Insert "This is the added text" and
a paragraph mark after the selection.

Which is not the most elegant way,
but I thought, I'd keep it simple.

Make sure to reset all search option beforehand.

' --------------------------------------

Faster, simpler, though a bit more advanced:

Sub Test45567A()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "Test"
.MatchCase = True
.Style = "Heading 1"
If .Execute Then ' just in case
Selection.Paragraphs(1).Range.InsertAfter _
"This is the added text" & Chr(13)
End If
End With
End Sub

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

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