|
-
Oct 29th, 2008, 05:53 AM
#1
Thread Starter
Lively Member
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!..
-
Oct 29th, 2008, 06:03 AM
#2
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
-
Oct 29th, 2008, 06:54 AM
#3
Thread Starter
Lively Member
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!..
-
Oct 29th, 2008, 04:03 PM
#4
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
-
Oct 30th, 2008, 04:02 AM
#5
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,}
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|