find replace in a vba macro

C

cjcpr

First I want to count the number of times the string "http" occurs in the
document. Then using that count I want to replace each http with a seq number
like - first http would be 'http01', second http would be http02 so on so
forth.

Could you please help me with a VB MAcro?
 
G

Greg Maxey

Sub ScratchMacro()
Dim findText As String
Dim startNum
Dim myRange As Range
Set myRange = ActiveDocument.Range
startNum = InputBox("Start sequential numbers at:")
findText = InputBox("Enter text to find")
With myRange.Find
.Text = findText
.MatchWholeWord = True
While .Execute
myRange.Text = myRange.Text & Format(startNum, "01")
startNum = startNum + 1
myRange.Collapse Direction:=wdCollapseEnd
Wend
End With
End Sub
 
D

Doug Robbins - Word MVP on news.microsoft.com

Use:

Selection.HomeKey wdStory
Selection.Find.ClearFormatting
Dim frange As Range
Dim i As Long
i = 1
With Selection.Find
Do While .Execute(FindText:="http", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False) = True
Selection.InsertAfter Format(i, "00")
Selection.Collapse wdCollapseEnd
i = i + 1
Loop
End With


--
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, originally posted via msnews.microsoft.com
 
H

Helmut Weber

Hi cjcpr,

well, I doubt,
whether all in one line (or paragraph) is what you expect.
My solution produces the following output:

03 http01://www.test01.com
http02://www.test02.com
http03://www.test03.com

Public Sub MyTest993()
Dim rDcm As Range
Dim i As Long
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "http"
While .Execute
i = i + 1
Wend
End With
ActiveDocument.Range.InsertBefore Format(i, "00") & " "
i = 0
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "http"
While .Execute
i = i + 1
rDcm.Text = rDcm.Text & Format(i, "00")
rDcm.Collapse Direction:=wdCollapseEnd
Wend
End With
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
C

cjcpr

thanks much. They have changed the requirement on me and swear that this is
final.


the expected output results should be this.

03 01 ,
02 ,
03


I tried to play with the macro you have given me over the weekend. With my
limited knowledge i couldn't get it to produce the results as listed above.

I know I am troubling you too much. Please help me out. Thanks in advance.
 
C

cjcpr

another problem i have noticed over the weekend was the links do not always
end with .com

it could be like http://www.test01.com/#p010019

The only constant thing i have noticed in the links is the http://wwww part

Thanks
cjcpr said:
thanks much. They have changed the requirement on me and swear that this is
final.


the expected output results should be this.

03 01 ,
02 ,
03


I tried to play with the macro you have given me over the weekend. With my
limited knowledge i couldn't get it to produce the results as listed above.

I know I am troubling you too much. Please help me out. Thanks in advance.


Helmut Weber said:
Hi cjcpr,


well, I doubt,
whether all in one line (or paragraph) is what you expect.
My solution produces the following output:

03 http01://www.test01.com
http02://www.test02.com
http03://www.test03.com

Public Sub MyTest993()
Dim rDcm As Range
Dim i As Long
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "http"
While .Execute
i = i + 1
Wend
End With
ActiveDocument.Range.InsertBefore Format(i, "00") & " "
i = 0
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "http"
While .Execute
i = i + 1
rDcm.Text = rDcm.Text & Format(i, "00")
rDcm.Collapse Direction:=wdCollapseEnd
Wend
End With
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
H

Helmut Weber

Hi cjcpr,
They have changed the requirement on me and swear that this is final.

I wonder whether "they" know their requirements at all.

If this is the input:
http://www.test01.com¶
http://www.test02.com¶
http://www.test03.com¶

then this:
Public Sub MyTest993()
Dim rDcm As Range
Dim i As Long
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "http"
While .Execute
i = i + 1
Wend
End With
ActiveDocument.Range.InsertBefore Format(i, "00") & " "
i = 0
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "http"
While .Execute
i = i + 1
rDcm.Select
rDcm.InsertBefore " " & Format(i, "00") & " , "
Else
selection.Characters.Last = _
"] " & Format(i, "00") & " [/url], "
End If
Stop
rDcm.Collapse Direction:=wdCollapseEnd
Stop
Wend
End With
End Sub

produces that output:

03 01 ,
02
, 03 , ¶

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
C

cjcpr

LOL....

This is for an external Client. Amazing results. Tanks much. I truly
appreciate this.

Helmut Weber said:
Hi cjcpr,
They have changed the requirement on me and swear that this is final.

I wonder whether "they" know their requirements at all.

If this is the input:
http://www.test01.com¶
http://www.test02.com¶
http://www.test03.com¶

then this:
Public Sub MyTest993()
Dim rDcm As Range
Dim i As Long
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "http"
While .Execute
i = i + 1
Wend
End With
ActiveDocument.Range.InsertBefore Format(i, "00") & " "
i = 0
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "http"
While .Execute
i = i + 1
rDcm.Select
rDcm.InsertBefore " " & Format(i, "00") & " , "
Else
selection.Characters.Last = _
"] " & Format(i, "00") & " [/url], "
End If
Stop
rDcm.Collapse Direction:=wdCollapseEnd
Stop
Wend
End With
End Sub

produces that output:

03 01 ,
02
, 03 , ¶

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
C

cjcpr

when i run it in word, it stops with the STOP Stmt. Am I doing anything wrong?

cjcpr said:
LOL....

This is for an external Client. Amazing results. Tanks much. I truly
appreciate this.

Helmut Weber said:
Hi cjcpr,
They have changed the requirement on me and swear that this is final.

I wonder whether "they" know their requirements at all.

If this is the input:
http://www.test01.com¶
http://www.test02.com¶
http://www.test03.com¶

then this:
Public Sub MyTest993()
Dim rDcm As Range
Dim i As Long
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "http"
While .Execute
i = i + 1
Wend
End With
ActiveDocument.Range.InsertBefore Format(i, "00") & " "
i = 0
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "http"
While .Execute
i = i + 1
rDcm.Select
rDcm.InsertBefore " " & Format(i, "00") & " , "
Else
selection.Characters.Last = _
"] " & Format(i, "00") & " [/url], "
End If
Stop
rDcm.Collapse Direction:=wdCollapseEnd
Stop
Wend
End With
End Sub

produces that output:

03 01 ,
02
, 03 , ¶

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 

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