add text to a file from vba

Edwinq

Registered User.
Local time
Today, 12:17
Joined
Apr 8, 2010
Messages
25
Hi,
I'd like to add text to an existing (rtf) file. I tried the following, which does not work, no text is appended:

nextfilenum = freefile
Open FileName & fileExtension For Append As #nextFileNum
Print #nextFileNum, newfilecontents
Close nextfilenum

any suggestions ??

thnx in advance
edwin
 
i suspect an rtf file is not a normal text file - maybe you cannot just append to it - are you ignoring errors somehow, in your code? - i would have thought if the append failed, you would get an error message.
 
If have no "On error resume" anywhere ... no errors are shown ...
 
Yes i am sure it contains text (debug.print helped me there)
Should be some way to manipulate text in a word/rtf document.
I can replace text in this document, but whenever i try to add or replace a string with more characters than the original text i get stuck.
 
Are you saying that you can't add text after the last line in the file? You know that append is meant to add after the end of file?

Appending to rtf works for me. Just tried it.
 
thanks for your help vbaInet,

Did you try that with the same lines of code ??
 
Yep, with a hard coded file name. I also tried the Write variant of Print.
 
edwin

maybe the problem is in filename. this needs to include the full path.

try docmd.followhyperlink to the filename - see if it opens.
(or is it application.followhyperlink)

change your error handling to jump to a proper handler - then you will be able to see exactly what the error is.
 
Thnx for replies ... I could use a proper error handler, only.. there is no error. text is just not appended.
I will try the write variant.
...
 
I still don't get it ...
The first Print is done, The others aren't
Only oldFileContents is written to the file, Addtext and newfileContents2 are not written.
I am positive they are not empty.

Open FileName & "_" & Bestelnummer & "." & fileExtension For Output As #nextFileNum
Print #nextFileNum, oldFileContents
Print #nextFileNum, Addtext
Print #nextFileNum, newfilecontents2
Close nextFileNum

any ideas ?
 
here is a thought

maybe oldfilecontents includes a file terminator (is it chr(0) or chr(26)) - so you can't then read past the end of file
 
OK thanks,
Is there a way to add text to this file ? Or a way to remove such a terminator ?
The oldfilecontents are read from another .rtf file

edwin
 
But if all you're doing is appending to the rtf, it should just work. Perhaps you can upload one of your rtf's (if it doesn't contain sensitive data) for us to look at.
 
My files exceed maximum file sizes for uploading as attachments


edwin
 
Could the problem be the header and footer in the rtf ??

thnx
 
Make it short, like 10 lines for example. Test to see that your code doesn't work on this short file before uploading.
 

Users who are viewing this thread

Back
Top Bottom