|
-
Jan 27th, 2006, 06:36 AM
#1
Thread Starter
Junior Member
Reading Inputs from external file
Hi,
I am currently building a VBA app to help me convert a bunch of HTML.
i need to do a lot of "Find & Replace" but i do not want to hardcore the search and replace strings into the VBA coz the requirement changes from time to time.
I want to have a external file that contains the search keyword folowed by some seperator like "||" and then the replace keyword
eg:
<h1> || <header1>
<h2> || <header2> and so on...
When ever i run the tool i specify the input file and then the conversion should start.
How do i go about achieving that.
Please Help.
Thanks in advance
-
Jan 27th, 2006, 07:53 AM
#2
Re: Reading Inputs from external file
 Originally Posted by imatrox05
I am currently building a VBA app
What are you building it in? Access, Excel, Word...
BTW, you decision to go with a file based approach is the correct one. But I need to know you app of choice before giving any more advice.
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful 
-
Jan 27th, 2006, 10:22 PM
#3
Thread Starter
Junior Member
Re: Reading Inputs from external file
-
Jan 30th, 2006, 09:58 AM
#4
Re: Reading Inputs from external file
OK
The best way to do this is to use a TextStream from a FileSystemObject. Here's some code that will achieve this.
You will need to change the value of sPath to the fully qualified path for your reference file.
Also, make sure you have a reference to the MS Scripting Runtime library in your project, or else you will not be able to access the FileSystemObject.
VB Code:
Sub imatrox05_replace()
'Note: You will need a reference to the MS Scripting
' Runtime library to use the FileSystemObject
Dim fsReplaceData As Scripting.FileSystemObject
Dim sText As TextStream
Dim sPath As String
Dim sFullLine As String
Dim aLineArray() As String
'The fully qualified path to your lookup file
sPath = "C:\data\testreplace.txt"
'Create a new FileSystemObject
Set fsReplaceData = New Scripting.FileSystemObject
'Create a text stream from your FileSystemObject
Set sText = fsReplaceData.OpenTextFile(sPath, ForReading, False, TristateUseDefault)
'Loop through each line in the text stream
Do While Not sText.AtEndOfStream
'Get the complete line of text
sFullLine = sText.ReadLine
'split the line of text into a 1 dimensional array
'using || as the delimeter and
'only taking the first 2 values from the line
aLineArray = Split(sFullLine, "||", 2)
'Within the Active document find all instances of
'the first value in the Array and replace with
'the second value in the Array
With ActiveDocument.Range.Find
.Text = aLineArray(LBound(aLineArray))
.Replacement.Text = aLineArray(UBound(aLineArray))
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue, MatchWholeWord:=True
End With
Loop
Set sText = Nothing
Set fsReplaceData = Nothing
End Sub
Last edited by DKenny; Jan 30th, 2006 at 10:01 AM.
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful 
-
Feb 2nd, 2006, 06:01 AM
#5
Thread Starter
Junior Member
Re: Reading Inputs from external file
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|