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..
Re: How to read constants from txt file.?
Quote:
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
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,}
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 "
Re: How to read constants from txt file.?
here is a conversion for you
vb Code:
Open "C:\test\123.txt" For Input As 1
s = Input(LOF(1), #1)
Close 1
's = [0-9]{1,}[" & Chr(45) & "][0-9]{1,}
t = ""
sfnd = """ & chr("
pos = InStr(1, s, sfnd, vbTextCompare)
pos1 = InStr(pos + Len(sfnd), s, """")
t = Left(s, pos - 1) & Chr(Val(Mid(s, pos + Len(sfnd), InStr(pos + Len(sfnd), s, ")") - pos + Len(sfnd)))) & Mid(s, pos1 + 1)
't = [0-9]{1,}[-][0-9]{1,}