Searching for Text

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

master.investor.2005

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

I.)

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?

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.)

Thanks
 
J

Jean-Guy Marcil

(e-mail address removed) was telling us:
(e-mail address removed) nous racontait que :
Hi. I have two main issues in creating my macro for Word.

I.)

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
 
M

master.investor.2005

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
 
J

Jean-Guy Marcil

(e-mail address removed) was telling us:
(e-mail address removed) nous racontait que :
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

To make things easier for everybody (you, me, anyone reading this), next
time it might be helpful to describe the whole process, not just bits and
pieces, this way nobody wastes time writing/trying/testing code that is
ultimately doomed to failed since the one writing the code is testing it on
a totally different context.

Now, since you are writing that you can find the right table, but only need
to select it, try this:

After having placed the cursor (or range) in the right table, use this to
select it:

Selection.Tables(1).Select

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

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