Use PERL instead.
What you are doing is called "Parsing" to find the value that you want to change. Then you change it and rewrite the file.
If you post a sample of the data I will try and write a REGEX for you.
Then you can simply use a system() call from VB to execute the PERL script.
What it will do is open a file, parse, store the NEW values into an array, close the file, open the text file, parse, locate the OLD value, replace with the NEW values in the array, write text file, close.
Main Topics
Browse All Topics





by: SCDMETAPosted on 2004-11-20 at 00:38:57ID: 12632601
The following code isn't a complete solution, but it might get you moving in the right direction.
False, False, False, "", ""
owCodes owCodes owCodes
Fields.Add Range:=.Selection.Range, Name:="New_City" Fields.Add Range:=.Selection.Range, Name:="New_Last_Name"
This is a VB program that opens a document and replaces merge fields.
(You need to add a reference to Microsoft Word Object Library.)
You could modify this example by passing in the document to open, and a collection old and new fields.
Private Sub Command1_Click()
Dim word As word.Application
Dim field As String
Dim lastpos As Long
Set word = New word.Application
With word
.Visible = True
.Documents.Open """C:\Documents and Settings\TestField.doc""",
.ActiveDocument.Activate
.Selection.WholeStory
.Selection.Fields.ToggleSh
.Selection.Fields.ToggleSh
.Selection.HomeKey Unit:=wdStory
.Selection.GoTo What:=wdGoToField, Which:=wdGoToNext, Count:=1, Name:="MERGEFIELD"
.Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
If Asc(Mid$(.Selection.Text, 1, 1)) <> 171 Then
.Selection.HomeKey Unit:=wdStory
.Selection.WholeStory
.Selection.Fields.ToggleSh
End If
.Selection.HomeKey Unit:=wdStory
.Selection.GoTo What:=wdGoToField, Which:=wdGoToNext, Count:=1, Name:="MERGEFIELD"
.Selection.GoTo What:=wdGoToField, Which:=wdGoToPrevious, Count:=1, Name:="MERGEFIELD"
.Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
lastpos = .Selection.Start
Do
'Do Replace
field = .Selection.Text
If Asc(field) <> 13 Then
field = Mid$(field, 2)
field = Mid$(field, 1, Len(field) - 1)
Select Case field
Case "City"
.ActiveDocument.MailMerge.
Case "Last_Name"
.ActiveDocument.MailMerge.
End Select
End If
lastpos = .Selection.Start
.Selection.MoveRight Unit:=wdCharacter, Count:=1
.Selection.GoTo What:=wdGoToField, Which:=wdGoToNext, Count:=1, Name:="MERGEFIELD"
.Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Loop While lastpos <> .Selection.Start
.ActiveDocument.Save
End With
End Sub