ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Open file to write and read at the same time (https://www.excelbanter.com/excel-programming/277657-re-open-file-write-read-same-time.html)

Tom Ogilvy

Open file to write and read at the same time
 
http://support.microsoft.com/support...eio/fileio.asp
File Access with Visual Basic® for Applications

Pretty vague just to say it doesn't work and put up 17 lines of code.
Pehaps the above will give you some ideas.

--
Regards,
Tom Ogilvy

mandy_less wrote in message
...
Hi all,

I'm trying to read the first line of a text file and write some lines
to the same file if the file exists. Have tried the codes below but
doesnt work!

Start of code...

=============================================
hFile = FreeFile 'Get the next free file handle
nFile = FreeFile
attachmentpath = "C:\test.txt"

If Len(Dir(attachmentpath)) = 0 Then
Open attachmentpath For Output Access Write Shared As _
hFile
Print #hFile, "test msg" , strInput
Else
Open attachmentpath For Input Access Read Shared As _
nFile Line Input #nFile, strline
Close #nFile

Open attachmentpath For Output Access Write Lock Read _
Write As hFile
Print #hFile, "test msg" , strInput
End If

Close hFile
Close nFile
==============================================
End of code...

Thanks in advance



------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~ View and post usenet messages directly from http://www.ExcelForum.com/




mandy_less[_2_]

Open file to write and read at the same time
 
Hi again,

My intention is to read the 1st line from the text file using the
following code:

OPEN ATTACHMENTPATH FOR INPUT ACCESS READ SHARED AS _
NFILE LINE INPUT #NFILE, STRLINE
CLOSE #NFILE
'...(1) [/b]

AND WRITE TO THE SAME FILE USING THESE CODES:

[b]OPEN ATTACHMENTPATH FOR OUTPUT ACCESS WRITE LOCK READ _
WRITE AS HFILE
PRINT #HFILE, \"TEST MSG\" , STRINPUT
'...(2)

However, the message from (2) is not written to the text file even
though I have closed the file (1) and re-opened it for writing (2).

Thanks again!!!



------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~ View and post usenet messages directly from http://www.ExcelForum.com/


Tom Ogilvy

Open file to write and read at the same time
 
Notice how you use

#Nfile
in the first command

and just Hfile in the second.

shouldn't it be

#Hfile.

--
Regards,
Tom Ogilvy

"mandy_less" wrote in message
...
Hi again,

My intention is to read the 1st line from the text file using the
following code:

OPEN ATTACHMENTPATH FOR INPUT ACCESS READ SHARED AS _
NFILE LINE INPUT #NFILE, STRLINE
CLOSE #NFILE
'...(1) [/b]

AND WRITE TO THE SAME FILE USING THESE CODES:

[b]OPEN ATTACHMENTPATH FOR OUTPUT ACCESS WRITE LOCK READ _
WRITE AS HFILE
PRINT #HFILE, \"TEST MSG\" , STRINPUT
'...(2)

However, the message from (2) is not written to the text file even
though I have closed the file (1) and re-opened it for writing (2).

Thanks again!!!



------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~ View and post usenet messages directly from http://www.ExcelForum.com/





All times are GMT +1. The time now is 03:10 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com