Search for and Copy Selected Text Macro

  • Thread starter master.investor.2005
  • Start date
M

master.investor.2005

Below is an old message which I think needed to be reposted

---------------------------------------------------------------------------------------------------------------------------------------
Hi. I have two main issues in creating my macro for Word.


I basically want to copy text that is located in between two strings
of text. Could you guys provide a simple macro for me to do that?



Try something like this. It finds the first occurrence of a target
between
two strings, creates a string with it (if necessary), copies the
target,
then pastes the target at the end of the current document.

'_______________________________________
Sub FindBetweenTwoStrings()


Dim strOne As String
Dim strTwo As String
Dim strTarget As String


strOne = "Your first string of text"
strTwo = "Your second string of text"


With ActiveDocument.Range
With .Find
.Text = "(" & strOne & ")*(" & strTwo & ")"
.MatchWildcards = True
.Execute
If .Found Then
With .Parent
.MoveStart wdCharacter, Len(strOne)
.MoveEnd wdCharacter, -Len(strTwo)
strTarget = .Text
.Copy
End With
Else
MsgBox "The text was not found.", vbExclamation, _
"Not Found"
Exit Sub
End If
End With
End With
With ActiveDocument.Range
.Collapse wdCollapseEnd
.Paste
End With
End Sub
'_______________________________________


II). I want to know how I can specify a parameter in opening a file.
(I.e. Just changing four characters of the webaddress of the file.)


I have no idea what you are writing about for point II.
Normally, it is better to have two different threads (posts) for two
unrelated questions.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org


Reply » Rate this post: Text for clearing space


From: (e-mail address removed) - view profile
Date: Wed, Jul 12 2006 10:18 am
Email: (e-mail address removed)
Groups: microsoft.public.word.vba.general
Not yet ratedRating:
show options


Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Remove | Report Abuse | Find messages by this author


Hi and thanks Jean,

While your macro did copy and paste text, it did not copy the correct
text. To make myself clearer let me again outline what I am aiming to
do. Essentially I want to copy some tab separated tables from a large
RTF document. Luckily, most of these tables have a common format so I
should have no problem finding them within a given document. However,
for me to copy the tables I have to specify where they would
essentially begin and end. I thought by specificifying the first word
of the table's title and the last word of the table I could
successfully copy the entire table. Your macro, unfortunately, for some

odd reason does not copy the correct text. I am hoping maybe you or
anyone else for that matter now knows the situation at hand can devise
a macro to deal with my current situation.


Best and Thanks



- Hide quoted text -
- Show quoted text -

Jean-Guy Marcil said:
(e-mail address removed) was telling us:
(e-mail address removed) nous racontait que :




Try something like this. It finds the first occurrence of a target between
two strings, creates a string with it (if necessary), copies the target,
then pastes the target at the end of the current document.

'_______________________________________
Sub FindBetweenTwoStrings()

Dim strOne As String
Dim strTwo As String
Dim strTarget As String

strOne = "Your first string of text"
strTwo = "Your second string of text"

With ActiveDocument.Range
With .Find
.Text = "(" & strOne & ")*(" & strTwo & ")"
.MatchWildcards = True
.Execute
If .Found Then
With .Parent
.MoveStart wdCharacter, Len(strOne)
.MoveEnd wdCharacter, -Len(strTwo)
strTarget = .Text
.Copy
End With
Else
MsgBox "The text was not found.", vbExclamation, _
"Not Found"
Exit Sub
End If
End With
End With
With ActiveDocument.Range
.Collapse wdCollapseEnd
.Paste
End With
End Sub
'_______________________________________


I have no idea what you are writing about for point II.
Normally, it is better to have two different threads (posts) for two
unrelated questions.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org



Reply »
 

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