Search string and copy all text at the end of the section

J

jlashley

Hi all,
I need to search for a string in a document , select all the content
after this string until the end of the section and copy this
selection in a new document.
As I'm new in vba word, I don't know the best way to do this, so any
help would be very kind.

Many thanks in advance.

Regards
 
H

Helmut Weber

Hi jlashley,

not that simple for a beginner.

Search for a string.
If found then
remember it's end position (x1).
Search from there for a section break (chr(12).
if found then
remember the end of the found spot (x2).
Add a new document.
Set it's text to the text of the range from x1 to x2.

Sub Test1()
Dim x1 As Long
Dim x2 As Long
Dim oDc1 As Document
Dim oDc2 As Document
Dim rDc1 As Range
Set oDc1 = ActiveDocument
Set rDc1 = oDc1.Range
With rDc1.Find
.Text = "xxxx"
If .Execute Then
x1 = rDc1.End
rDc1.Start = x1
rDc1.End = ActiveDocument.Range.End
With rDc1.Find
.Text = Chr(12)
If .Execute Then
x2 = rDc1.End
Set oDc2 = Documents.Add
oDc2.Range.Text = oDc1.Range(x1, x2).text
End If
End With
End If
End With
End Sub

And many more ways to do that.

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Doug Robbins - Word MVP

Perhaps this is a bit simpler than Helmut's version:

Dim drange As Range
Dim target As Document
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Execute findText:="your string", MatchWildcards:=False,
Wrap:=wdFindStop, Forward:=True
Set drange = Selection.Range
drange.Start = drange.End 'Delete this line if you want the search
string included in the new document
drange.End = Selection.Sections(1).Range.End - 1
End With
Set target = Documents.Add
target.Range.FormattedText = drange.FormattedText


--
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
 
H

Helmut Weber

Hi Doug,
Perhaps this is a bit simpler than Helmut's version:

it is indeed.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

jlashley

Hello,

I try the two examples, and , both works fine !
Again, many thanks.

Best regards !
 

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