Results 1 to 5 of 5

Thread: How to read constants from txt file.?

  1. #1

    Thread Starter
    Lively Member ashokkumard's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    119

    Cool How to read constants from txt file.?

    Hi all

    I want to find some text by reading from a text file. I had stored the below information in a text file

    Code:
    [0-9]{1,}[" & chr(45) & "][0-9]{1,}
    and want to read it thro' code and find the same in a word document. Here instead of hyphen i want to store the ascii value and retrieve from the file. Please have a look at my code.

    Code:
    Private Sub ReadFromFile()
    Dim sStoredText As String
    Dim sFilePath As String, aTest() As String
    Dim sFile As File, ReadFile As TextStream
    Dim rngTest As Range
        
        sFilePath = "D:\Test.txt"
        Set FSO = New FileSystemObject
        If FSO.FileExists(sFilePath) Then
            Set sFile = FSO.GetFile(sFilePath)
            Set ReadFile = sFile.OpenAsTextStream(ForReading)
            Do While Not ReadFile.AtEndOfStream
                sStoredText = ReadFile.ReadLine
            Loop
        End If
        Set rngTest = ActiveDocument.Range
        rngTest.Find.ClearFormatting
        rngTest.Find.Execute findtext:=sStoredText, MatchWildcards:=True
        If rngTest.Find.Found Then rngTest.Select
        MsgBox sStoredText
    End Sub
    the chr value of hyphen didn't change in that string variable. Plz advice me..
    Thanks
    Ashok

    Nothing is permanent except changes!..

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to read constants from txt file.?

    sStoredText = ReadFile.ReadLine
    at the end of the loop the string only contains the last line of the file, often the last line is an empty line, as the last line printed to the file will contain a newline character at the end
    possibly try
    sStoredText = sStoredText & ReadFile.ReadLine
    or check your text file to see if it has empty line at end

    i doubt you can pass a chr that way into a variable without converting it
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Lively Member ashokkumard's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    119

    Re: How to read constants from txt file.?

    I have only one line in tat text file, and moreover that is the line how can i retrieve the chr(45) not as a sitring as it is, but want to store the whole line in a string variable.

    [0-9]{1,}[" & chr(45) & "][0-9]{1,}

    i want the above as

    [0-9]{1,}[-][0-9]{1,}
    Thanks
    Ashok

    Nothing is permanent except changes!..

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to read constants from txt file.?

    find & chr then the following & in the string
    then replace that part of the string with chr(45) or whatever value for chr
    you may also need to replace the " at each end, in which case find " & chr and the following "
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to read constants from txt file.?

    here is a conversion for you
    vb Code:
    1. Open "C:\test\123.txt" For Input As 1
    2. s = Input(LOF(1), #1)
    3. Close 1
    4. 's = [0-9]{1,}[" & Chr(45) & "][0-9]{1,}
    5. t = ""
    6. sfnd = """ & chr("
    7. pos = InStr(1, s, sfnd, vbTextCompare)
    8. pos1 = InStr(pos + Len(sfnd), s, """")
    9. t = Left(s, pos - 1) & Chr(Val(Mid(s, pos + Len(sfnd), InStr(pos + Len(sfnd), s, ")") - pos + Len(sfnd)))) & Mid(s, pos1 + 1)
    10. 't = [0-9]{1,}[-][0-9]{1,}
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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