Results 1 to 5 of 5

Thread: Reading Inputs from external file

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2005
    Posts
    24

    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

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Reading Inputs from external file

    Quote 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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2005
    Posts
    24

    Re: Reading Inputs from external file

    Word

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    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:
    1. Sub imatrox05_replace()
    2. 'Note:  You will need a reference to the MS Scripting
    3. '       Runtime library to use the FileSystemObject
    4. Dim fsReplaceData As Scripting.FileSystemObject
    5. Dim sText As TextStream
    6. Dim sPath As String
    7. Dim sFullLine As String
    8. Dim aLineArray() As String
    9.    
    10.     'The fully qualified path to your lookup file
    11.     sPath = "C:\data\testreplace.txt"
    12.    
    13.     'Create a new FileSystemObject
    14.     Set fsReplaceData = New Scripting.FileSystemObject
    15.    
    16.     'Create a text stream from your FileSystemObject
    17.     Set sText = fsReplaceData.OpenTextFile(sPath, ForReading, False, TristateUseDefault)
    18.    
    19.     'Loop through each line in the text stream
    20.     Do While Not sText.AtEndOfStream
    21.        
    22.         'Get the complete line of text
    23.         sFullLine = sText.ReadLine
    24.        
    25.         'split the line of text into a 1 dimensional array
    26.         'using || as the delimeter and
    27.         'only taking the first 2 values from the line
    28.         aLineArray = Split(sFullLine, "||", 2)
    29.        
    30.         'Within the Active document find all instances of
    31.         'the first value in the Array and replace with
    32.         'the second value in the Array
    33.         With ActiveDocument.Range.Find
    34.             .Text = aLineArray(LBound(aLineArray))
    35.             .Replacement.Text = aLineArray(UBound(aLineArray))
    36.             .Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue, MatchWholeWord:=True
    37.         End With
    38.     Loop
    39.    
    40.     Set sText = Nothing
    41.     Set fsReplaceData = Nothing
    42. 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

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2005
    Posts
    24

    Re: Reading Inputs from external file

    Thanks DKenny

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width