Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
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/



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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/

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default 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/



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how can i open the write protected excel file? Vipul Shukla Excel Discussion (Misc queries) 1 November 7th 08 08:33 AM
HOW TO OPEN A WRITE PROTECTED FILE (USB) Wendyanne Excel Discussion (Misc queries) 2 August 16th 06 01:38 PM
I have a read only xl file, I need it to be read and write drama queen Excel Discussion (Misc queries) 3 July 1st 06 12:25 AM
read only file-need to write judithbear Excel Discussion (Misc queries) 4 January 17th 06 03:31 PM
How can a file be converted from Read-Only to Read/Write Jim in Apopka Excel Discussion (Misc queries) 2 November 19th 05 04:59 PM


All times are GMT +1. The time now is 02:53 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"