View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default 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/