Search and replace within a selection

J

JWirt

I want to write a procedure that will search through a flat text file to find
fields with a certain fieldname (i.e., "KE[ ") , extend the selection to the
entire field (i.e., until the next vbCRLF), and then replace all commas in
that selection with semi-colons.

A basic procedure that will do this is shown below, but how can I replace
text within the selected field? Set the current selection to a selection
object and then search and replace in that object?

Public Sub ReplaceSelwSemiColons()
Dim FoundField as Selection

Selection.HomeKey Unit:=wdStory 'go to beginning of document
Do
With Selection.Find
.Text = "KE[ " 'search for next field named KE[^
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = True
End With
Selection.Find.Execute
If Selection.Find.Found = True Then
Selection.EndOf Unit:=wdParagraph, Extend:=wdExtend
'if KE[ is found, extend the selection to the entire field

{now, what goes in here to replace all commas with a
semicolon?}
Set FoundField=Selection
.....?

End If
Selection.Collapse Direction:=wdCollapseEnd
Loop 'loop back to find next instance of the field

End Sub


John Wirt
 
H

Helmut Weber

Hi JWirt

like that:

Sub Test007()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "KE\[ *^13"
.MatchWildcards = True
While .Execute
rDcm.Text = Replace(rDcm.Text, ",", ";")
Wend
End With
End Sub

The range rDcm shrinks to the found spot
and extends automatically to end of the doc.


--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
J

JWirt

Hmmm...Thank you.
What is the

While .Execute
rDcm.Text = Replace(rDcm.Text, ",", ";")
Wend

structure? It repetitively executes the find and then the Replace text
assignment?

So the logic is that, first the Active Document is set to a range. Then the
document searched as a text string is for instances of the text string "KE\[
*^13", where "*" is the wildcard character (and can be 1 to N characters with
N possibly as big as the number of characters in the document). Within each
instance found the the .Execute with the While...Wend loop looks for all ","s
and replaces them with ";"s.

Pretty compact.

I didn't know this about the structure of the Find command. It can be
repetitively executed within a found text without assigning the found text to
a selection.

Where can I read about this?

Thanks.

John
 
J

JWirt

Now another question following this.

Suppose I want to replace all semicolons in another field "AU[ " with,

(vbCRLF)AU[space

....in words by a hard CRLF followed by the field name AU[ with a space
after it.

I tried this,

rDcm.Text = Replace(rDcm.Text, ";", "^13AU\[ ")

but it didn't work. This statement inserted

"^13AU\[

for every semicolon.

And, how do I insert a CRLF in the text instead of ^13? I should insert
^13^10? And instead of "AU\[ " I should insert what?

Thank you.

John Wirt
Wash, DC
 
H

Helmut Weber

Hi John,

you got it all perfectly right,
What is the
While .Execute
rDcm.Text = Replace(rDcm.Text, ",", ";")
Wend
structure?
It repetitively executes the find
and then the Replace text assignment? yes.

Pretty compact.
Yes, that is the fun part.
I didn't know this about the structure of the Find command. It can be
repetitively executed within a found text without assigning the found text to
a selection. Yes.

Where can I read about this?
I don't know of a comprehensive source covering
range vs. selection issues.
I've learned it the hard way.
Googling for my decent name and "range"
might provide you with some more information.
I was busy, I'm finding 1720 postings. ;-)

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
H

Helmut Weber

Hi John,
Now another question following this.
I was afraid of that beforehand.

Read fellow MVP Greg Maxey's remarks on
"NEVER USE ^13 IN THE "REPLACE WITH"
http://gregmaxey.mvps.org/Replace_With_Paragraph.htm

Maybe this will help you,
but i'm not sure.

Sub Test007A()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "KE\[ *^13"
.MatchWildcards = True
While .Execute
rDcm.Text = Replace(rDcm.Text, ";", vbCrLf & "KE\[ ")
Wend
End With
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
J

John Wirt

Helmut,

Yes, I just figured out the vbCRLF myself about two minutes ago and came
on here to report it but I see you figured it out right away. Not too
difficult. I'm a little rusty.

But what is the "\" for? It keys that a special character follows?

I really appreciate your help.

John


Helmut said:
Hi John,
Now another question following this.
I was afraid of that beforehand.

Read fellow MVP Greg Maxey's remarks on
"NEVER USE ^13 IN THE "REPLACE WITH"
http://gregmaxey.mvps.org/Replace_With_Paragraph.htm

Maybe this will help you,
but i'm not sure.

Sub Test007A()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "KE\[ *^13"
.MatchWildcards = True
While .Execute
rDcm.Text = Replace(rDcm.Text, ";", vbCrLf & "KE\[ ")
Wend
End With
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
F

fumei via OfficeKB.com

Try:

Public Sub SeparateStartEndPagesA()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "PP\[ *^13"
.MatchWildcards = True
While .Execute
rDcm.Text = Replace(rDcm.Text, "PP[", "SP[")
rDcm.Text = Replace(rDcm.Text, "-", vbCrLf & "EP[ ")
rDcm.Collapse Direction:=wdCollapseEnd
Wend
End With
End Sub


BTW: why use Selection.HomeKey Unit:=wdStory...if you are not using
Selection anyway? Unless of course there is a reason to move the cursor to
the start of the document. However, in terms of running the procedure
instructions, it is not needed at all. The code uses rDcm as a Range. The
Selection is not relevant.

John said:
Helmut,

I have run into a program in applying your coding ideas in the procedure
I am working on.

What I am doing is writing some VBA code to reformat a large number of
citations to journal articles in the academic literature in such a
record form:

TY[ JOURNAL
TI[ When Bigger Is Not Better: Family Size, Parental Resources, and
Children's Educational Performance
AU[ Smith,Eugene
AU[ Downey,Douglas B.
JO[ American Sociological Review
VO[ 60
NO[ 5
DA[ Oct., 1995
PP[ 746-761
WE[
http://links.jstor.org/sici?sici=0003-1224(199510)60:5<746:WBINBF>2.0.CO;2-T
IN[ 00031224
AB[ Although the inverse relationship between the number of siblings and
children's educational performance has been well established,
explanations for this relationship remain primitive. One explanation,
resource dilution, posits that parents have finite levels of resources
(time, energy, money, etc.) and that these resources are diluted among
children as ...snipped out to shorten...whether the resource is
interpersonal or economic. Second, parental resources explain most or
all of the inverse relationship between sibship size and educational
outcomes. Finally, interactions between sibship size and parental
resources support the dilution model as children benefit less from
certain parental resources when they have many versus few siblings.
KE[ Family background
KE[ Parental education
KE[ Achievement
KE[ Siblings
ER[

There are several hundred of these in a .txt file which I load into Word
and run the code I am writing with your help.

So far, with your code I have reformatted the AU[ (author) and KE[
(keyword) fields

The next step is to reformat the article pages specified in PP[. I want
to split them into two separate fields, setting the first number before
the hyphen to the Start Page (SP) and the second number to the End Page
(EP). ER[ is the end of record symbol.

I wrote this code to

Public Sub SeparateStartEndPagesA()
Selection.HomeKey Unit:=wdStory
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "PP\[ *^13"
.MatchWildcards = True
While .Execute
rDcm.Text = Replace(rDcm.Text, "PP[", "SP[")
rDcm.Text = Replace(rDcm.Text, "-", vbCrLf & "EP[ ")
Wend
End With
End Sub

I found that this code only converts the first PP[ field and then stops.
Only one record is changed.

By the subroutine again, the second record is changed. And so on. For
some reason, the While-.Execute-Wend loop stops working.

To solve this problem, I tried rewriting this PP[ code as two separate
operations:

Public Sub SeparateStartEndPagesA()
Selection.HomeKey Unit:=wdStory
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "PP\[ *^13"
.MatchWildcards = True
While .Execute
rDcm.Text = Replace(rDcm.Text, "PP[", "SP[")
Wend
End With
End Sub

Public Sub SeparateStartEndPagesB()
Selection.HomeKey Unit:=wdStory
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "SP\[ *^13"
.MatchWildcards = True
While .Execute
rDcm.Text = Replace(rDcm.Text, "-", vbCrLf & "EP[ ")
Wend
End With
End Sub

Same problem. As before, the PP[ field is reformatted only in the first
record (the first one where PP[ is found).

I don't understand this. Why does the While-.Execute-Wend loop reformat
the AU[ field but not the PP[ field.

When I used your code to split the authors into separate fields for each
author, it worked fine:

Public Sub ReplaceSemiColonswFieldInAU()
Dim rDcm As Range
Selection.HomeKey Unit:=wdStory
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "AU\[ *^13"
.MatchWildcards = True
While .Execute
rDcm.Text = Replace(rDcm.Text, ";", vbCrLf & "AU\[ ")
Wend
End With
End Sub

Originally, the AU[ field for the record at the beginning of this
message was like this:

AU[ Smith, Eugene ; Downey, Douglas B.

When I ran the code just above for ReplaceSemiColonswFieldInAU, it
ripped through all of the records and parsed the authors in each one (up
to four authors). Why does the same code as written for the AU[ field
stop one record in reformating the PP{ field?

Also, as you can see above, I found that I had to remove the "\" from
the Replace text statement for the PP[ field but not the AU[ field.

Thanks again.

John Wirt
 
R

Ralf Paugstadt

Hallo Herr Wirt,
ich bin auf Ihr Word VBA Beipiel unten gesto?en, das mir recht elegant erscheint.

In meiner kleinen Patentabteilung suche ich hin und wieder einmal einen kleinen Word VBA Trick, um unsere Arbeit zu erleichtern.

K?nnten Sie sich vorstellen uns hier zu helfen?

Danke Ralf Paugstadt
I want to write a procedure that will search through a flat text file to find
fields with a certain fieldname (i.e., "KE[ ") , extend the selection to the
entire field (i.e., until the next vbCRLF), and then replace all commas in
that selection with semi-colons.

A basic procedure that will do this is shown below, but how can I replace
text within the selected field? Set the current selection to a selection
object and then search and replace in that object?

Public Sub ReplaceSelwSemiColons()
Dim FoundField as Selection

Selection.HomeKey Unit:=wdStory 'go to beginning of document
Do
With Selection.Find
.Text = "KE[ " 'search for next field named KE[^
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = True
End With
Selection.Find.Execute
If Selection.Find.Found = True Then
Selection.EndOf Unit:=wdParagraph, Extend:=wdExtend
'if KE[ is found, extend the selection to the entire field

{now, what goes in here to replace all commas with a
semicolon?}
Set FoundField=Selection
.....?

End If
Selection.Collapse Direction:=wdCollapseEnd
Loop 'loop back to find next instance of the field

End Sub


John Wirt
On Sunday, December 30, 2007 6:14 PM Helmut Weber wrote:
Hi JWirt

like that:

Sub Test007()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "KE\[ *^13"
.MatchWildcards = True
While .Execute
rDcm.Text = Replace(rDcm.Text, ",", ";")
Wend
End With
End Sub

The range rDcm shrinks to the found spot
and extends automatically to end of the doc.


--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
Thank you.
What is the

While .Execute
rDcm.Text = Replace(rDcm.Text, ",", ";")
Wend

structure? It repetitively executes the find and then the Replace text
assignment?

So the logic is that, first the Active Document is set to a range. Then the
document searched as a text string is for instances of the text string "KE\[
*^13", where "*" is the wildcard character (and can be 1 to N characters with
N possibly as big as the number of characters in the document). Within each
instance found the the .Execute with the While...Wend loop looks for all ","s
and replaces them with ";"s.

Pretty compact.

I didn't know this about the structure of the Find command. It can be
repetitively executed within a found text without assigning the found text to
a selection.

Where can I read about this?

Thanks.

John

"Helmut Weber" wrote:
On Sunday, December 30, 2007 8:17 PM JWir wrote:
Now another question following this.

Suppose I want to replace all semicolons in another field "AU[ " with,

(vbCRLF)AU[space

...in words by a hard CRLF followed by the field name AU[ with a space
after it.

I tried this,

rDcm.Text = Replace(rDcm.Text, ";", "^13AU\[ ")

but it didn't work. This statement inserted

"^13AU\[

for every semicolon.

And, how do I insert a CRLF in the text instead of ^13? I should insert
^13^10? And instead of "AU\[ " I should insert what?

Thank you.

John Wirt
Wash, DC
On Sunday, December 30, 2007 9:10 PM Helmut Weber wrote:
Hi John,

I was afraid of that beforehand.

Read fellow MVP Greg Maxey's remarks on
"NEVER USE ^13 IN THE "REPLACE WITH"
http://gregmaxey.mvps.org/Replace_With_Paragraph.htm

Maybe this will help you,
but i'm not sure.

Sub Test007A()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "KE\[ *^13"
.MatchWildcards = True
While .Execute
rDcm.Text = Replace(rDcm.Text, ";", vbCrLf & "KE\[ ")
Wend
End With
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
Public Sub SeparateStartEndPagesA()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "PP\[ *^13"
.MatchWildcards = True
While .Execute
rDcm.Text = Replace(rDcm.Text, "PP[", "SP[")
rDcm.Text = Replace(rDcm.Text, "-", vbCrLf & "EP[ ")
rDcm.Collapse Direction:=wdCollapseEnd
Wend
End With
End Sub


BTW: why use Selection.HomeKey Unit:=wdStory...if you are not using
Selection anyway? Unless of course there is a reason to move the cursor to
the start of the document. However, in terms of running the procedure
instructions, it is not needed at all. The code uses rDcm as a Range. The
Selection is not relevant.

John Wirt wrote:
 

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