Read file and String Replace
Moderators: JRL, Dorian (MJT support)
Read file and String Replace
I have a Word Doc (rtf) file that I want to search and replace text in and then have the document open. Opening Word and using search and replace is not an option.
I used the rtf format instead of Word as the Word format seems to use some special Charactors .
I can currently read each line and use MS's Stringreplace to replace the data and write the line. There are numerous things to be replaced so it is a matter of reading a line, search the line for each thing to be replaced and write the line. No problem it works. The difficulty is that it takes quite a while to do it on a thirty page file with 10 items to be replaced.
However, if I use Readfile and perform the action it is almost instant. Howver I get a Syntax error on line 10 and 14 on each of the files that I try. Even though the error appears it seems to be working correctly.
Is there a way to disable the Error Message? or another way of doing it?
I have tried VBScript and get errors as well. With VBScript however, it does not generate the output, with MS it does.
I converted one of the files to a plain text file and recieved the Syntax error on line 12.
Any suggestions, this is an ideal way to fill out forms if it can be made to work.
I used the rtf format instead of Word as the Word format seems to use some special Charactors .
I can currently read each line and use MS's Stringreplace to replace the data and write the line. There are numerous things to be replaced so it is a matter of reading a line, search the line for each thing to be replaced and write the line. No problem it works. The difficulty is that it takes quite a while to do it on a thirty page file with 10 items to be replaced.
However, if I use Readfile and perform the action it is almost instant. Howver I get a Syntax error on line 10 and 14 on each of the files that I try. Even though the error appears it seems to be working correctly.
Is there a way to disable the Error Message? or another way of doing it?
I have tried VBScript and get errors as well. With VBScript however, it does not generate the output, with MS it does.
I converted one of the files to a plain text file and recieved the Syntax error on line 12.
Any suggestions, this is an ideal way to fill out forms if it can be made to work.
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
What's the syntax error?
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
Syntax error with Readfile
Marcus,
Sometimes it says Synatx error Line 8 and later it may say Syntax Error line 10 however if I run this
Input>filetoread,Browse to File to be Updated
ReadFile>%filetoread%,fileread
DeleteFile>%filetoread%
WriteLn>%filetoread%,result,%fileread%
Then ReadFile> works fine. Naturally all the syntas errors appear when this script is run, but it only happens once. I am now going to try JRL's suggestion.
Sometimes it says Synatx error Line 8 and later it may say Syntax Error line 10 however if I run this
Input>filetoread,Browse to File to be Updated
ReadFile>%filetoread%,fileread
DeleteFile>%filetoread%
WriteLn>%filetoread%,result,%fileread%
Then ReadFile> works fine. Naturally all the syntas errors appear when this script is run, but it only happens once. I am now going to try JRL's suggestion.
JRLs Solution to ReadFile Error
Well it sort of works, I don't get the error anymore when it reads the file, thank you. However, now I get the same message when I write the file.
It seems if Let>IGNOREERRORS=1 works with Readfile> but not with WriteLn>. I don't get it.
It seems if Let>IGNOREERRORS=1 works with Readfile> but not with WriteLn>. I don't get it.
Write whole file
It would not write it. Just a plain read the file and write the file.
Without the Let>IGNOREERRORS=1 line I get errors on reading the file.
With the line I get errors on writing the file. So Let>IGNOREERRORS=1 fixed one issue but caused another.
Without the Let>IGNOREERRORS=1 line I get errors on reading the file.
With the line I get errors on writing the file. So Let>IGNOREERRORS=1 fixed one issue but caused another.
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Can you send your script? Syntax error line 8 doesn't tell me much without seeing what line 8 is
That sounds like a VBScript syntax error. If the file has control characters, line breaks, etc, then depending on what your VBScript is doing that could cause the syntax error. E.g. if the VBScript is trying to read it in as a string but there are quotes or line breaks in the file then obviously that would cause a syntax error.

Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
Readfile error
Marcus
Input>filetoread,Browse to File to be Updated
ReadFile>%filetoread%,fileread
DeleteFile>%filetoread%
WriteLn>%filetoread%,result,%fileread%
This will generate the errors with a Word file. I have tried it on a half a dozen of them. What is strange is two fold. Sometimes it says Syntax Error Line 8, If I then open the file (in Word) again and save it, when run again it will still generate a Syntax error but the line number will be different.
But, once I run the script above, I have a new file. The new file does not generate the Syntax Error when read or written.
I have opened the Word Files in Notepad in hopes of finding something that did not look right, but saw nothing unusual.
It seems like there is some hidden character in the Word file that is causing this. But I don’t know what it can be. It does not seem to be something that is necessary as the formatting of the document remains the same.
Input>filetoread,Browse to File to be Updated
ReadFile>%filetoread%,fileread
DeleteFile>%filetoread%
WriteLn>%filetoread%,result,%fileread%
This will generate the errors with a Word file. I have tried it on a half a dozen of them. What is strange is two fold. Sometimes it says Syntax Error Line 8, If I then open the file (in Word) again and save it, when run again it will still generate a Syntax error but the line number will be different.
But, once I run the script above, I have a new file. The new file does not generate the Syntax Error when read or written.
I have opened the Word Files in Notepad in hopes of finding something that did not look right, but saw nothing unusual.
It seems like there is some hidden character in the Word file that is causing this. But I don’t know what it can be. It does not seem to be something that is necessary as the formatting of the document remains the same.
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Odd. There are only 4 lines. So I don't understand why you'd get an error on line 8.
Also, bear in mind that Word files are binary. ReadFile and WriteLn are designed to work with ascii text files.
Also, bear in mind that Word files are binary. ReadFile and WriteLn are designed to work with ascii text files.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
Word Binary File
I thought it was generating the Syntax error on the line that was being read. I say these are Word files, however, they are really Word-rtf files, which do not seem to be binary.
I tried to use the read and write line method on a standard .doc file, but if failed because as you stated it is binary. So I opted to use rtf, which does not appear to be binary.
It seems as Word is putting something in the file that is not necessary and it is causing the issue.
The reason I say it is not necessary, is if I use the script above and rewrite the file it works. So, after a change is made in the template (rtf file) the script above will have to clean it up before it is used, which I don't mind. The only part that I do mind is this is for a client and when they see the syntax error message they will think something is wrong. That is why I liked the idea of turning off the message but it didn't work when it came to writing the file.
The other part that sort of bothers me is that Readln does work and readfile generates these errors. I don't believe Readln will read and write a binary file, which also tells me these are not binary.
I tried to use the read and write line method on a standard .doc file, but if failed because as you stated it is binary. So I opted to use rtf, which does not appear to be binary.
It seems as Word is putting something in the file that is not necessary and it is causing the issue.
The reason I say it is not necessary, is if I use the script above and rewrite the file it works. So, after a change is made in the template (rtf file) the script above will have to clean it up before it is used, which I don't mind. The only part that I do mind is this is for a client and when they see the syntax error message they will think something is wrong. That is why I liked the idea of turning off the message but it didn't work when it came to writing the file.
The other part that sort of bothers me is that Readln does work and readfile generates these errors. I don't believe Readln will read and write a binary file, which also tells me these are not binary.
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Can you send me an .rtf file that creates this error. I'll use the code you posted to replicate. Confirm that your code is just those 4 lines.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
Read and Write Line
Marcus,
Below is the link to a test file, and here is script (the portion that has issues). On the first run you will get the syntax error message. Run it a second time on the same file you will not.
Input>filetoread,Browse to File to be Updated
ReadFile>%filetoread%,fileread
DeleteFile>%filetoread%
WriteLn>%filetoread%,result,%fileread%
http://www.edocfile.info\testGarn-Answer.doc
Below is the link to a test file, and here is script (the portion that has issues). On the first run you will get the syntax error message. Run it a second time on the same file you will not.
Input>filetoread,Browse to File to be Updated
ReadFile>%filetoread%,fileread
DeleteFile>%filetoread%
WriteLn>%filetoread%,result,%fileread%
http://www.edocfile.info\testGarn-Answer.doc
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Change your code to the following and I think you'll find it works:
The % symbols aren't needed on that last line. In fact they're not needed anywhere above. RTF starts and ends with "{" and "}" symbols and I think that is confusing the complex expression parser.
Code: Select all
Input>filetoread,Browse to File to be Updated
ReadFile>%filetoread%,fileread
DeleteFile>%filetoread%
WriteLn>%filetoread%,result,fileread
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
trouble with reading an rtf file
Marcus,
I made the changes and still get the same results. You mentioned the rtf file starting and ending with the "{". Once this script is run once, the errors no longer exist and the output file does have the "{" in it. So it leads me to beleive that it not the issue.
I made the changes and still get the same results. You mentioned the rtf file starting and ending with the "{". Once this script is run once, the errors no longer exist and the output file does have the "{" in it. So it leads me to beleive that it not the issue.