Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Old May 4th 19, 11:12 PM posted to microsoft.public.excel.programming
external usenet poster
 
First recorded activity by ExcelBanter: May 2019
Posts: 22
Default Error message: "Bad file name or number"

I know this code has worked for me before on other machines, but does anyone see a problem with this code:

'------------------------
Public Sub MyFileReader()

Dim f As Integer
Dim sFile As String

sFile = "C:\data.txt"

Open sFile For Input As f

Close f

End Sub
'-----------------------

The error message that I get is: "Bad file name or number".
And yes, the data.txt file exists on the root C: drive and the file is correctly named.

- Rob

  #2   Report Post  
Old May 5th 19, 01:20 AM posted to microsoft.public.excel.programming
external usenet poster
 
First recorded activity by ExcelBanter: May 2019
Posts: 22
Default Error message: "Bad file name or number"

Never mind... I was missing the darn FreeFile() line. Duh!
  #3   Report Post  
Old May 5th 19, 03:41 AM posted to microsoft.public.excel.programming
external usenet poster
 
First recorded activity by ExcelBanter: May 2019
Posts: 22
Default Error message: "Bad file name or number"

But the code doesn't even work when I use FreeFile(). What's wrong he

Public Sub ReadFile_OpenMethod()

Dim f As Integer
Dim sFile As String
Dim sTxt As String

f = FreeFile()

sFile = "C:\data.txt"

Open sFile For Input As f

While Not EOF(f)
Line Input #f, sTxt
Wend

Close f

End Sub



The error message is "File not found", but it's in the correct directory?
  #4   Report Post  
Old May 5th 19, 06:16 PM posted to microsoft.public.excel.programming
external usenet poster
 
First recorded activity by ExcelBanter: Apr 2015
Posts: 1,055
Default Error message: "Bad file name or number"

You should implement reusable procedures for file I/O operations so you don't
have to code for it every time. Here's what I use; - it just requires a
filename to read/write text, optionally whether to add to the file when
writing.

Function ReadTextFile$(Filename$)
' Reads large amounts of data from a text file in one single step.
Dim iNum%
On Error GoTo ErrHandler
iNum = FreeFile(): Open Filename For Input As #iNum
ReadTextFile = Space$(LOF(iNum))
ReadTextFile = Input(LOF(iNum), iNum)

ErrHandler:
Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description
End Function 'ReadTextFile()

Sub WriteTextFile(TextOut$, Filename$, _
Optional AppendMode As Boolean = False)
' Reusable procedure that Writes/Overwrites or Appends
' large amounts of data to a Text file in one single step.
' **Does not create a blank line at the end of the file**
Dim iNum%
On Error GoTo ErrHandler
iNum = FreeFile()
If AppendMode Then
Open Filename For Append As #iNum: Print #iNum, vbCrLf & TextOut;
Else
Open Filename For Output As #iNum: Print #iNum, TextOut;
End If

ErrHandler:
Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description
End Sub 'WriteTextFile()

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
  #5   Report Post  
Old May 5th 19, 06:22 PM posted to microsoft.public.excel.programming
external usenet poster
 
First recorded activity by ExcelBanter: Apr 2015
Posts: 1,055
Default Error message: "Bad file name or number"

You should implement reusable procedures for file I/O operations so you don't
have to code for it every time. Here's what I use; - it just requires a
filename to read/write text, optionally whether to add to the file when
writing.

Function ReadTextFile$(Filename$)
' Reads large amounts of data from a text file in one single step.
Dim iNum%
On Error GoTo ErrHandler
iNum = FreeFile(): Open Filename For Input As #iNum
ReadTextFile = Space$(LOF(iNum))
ReadTextFile = Input(LOF(iNum), iNum)

ErrHandler:
Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description
End Function 'ReadTextFile()

Sub WriteTextFile(TextOut$, Filename$, _
Optional AppendMode As Boolean = False)
' Reusable procedure that Writes/Overwrites or Appends
' large amounts of data to a Text file in one single step.
' **Does not create a blank line at the end of the file**
Dim iNum%
On Error GoTo ErrHandler
iNum = FreeFile()
If AppendMode Then
Open Filename For Append As #iNum: Print #iNum, vbCrLf & TextOut;
Else
Open Filename For Output As #iNum: Print #iNum, TextOut;
End If

ErrHandler:
Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description
End Sub 'WriteTextFile()


Note that WriteTextFile does not insert a blank line at the end of the file and
so is why a new line is prepended to the text when adding to an existing file.
Otherwise the file is overwritten!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #6   Report Post  
Old May 6th 19, 01:56 AM posted to microsoft.public.excel.programming
external usenet poster
 
First recorded activity by ExcelBanter: Apr 2015
Posts: 1,055
Default Error message: "Bad file name or number"

Usage example:

Sub DoThis()
Dim sTextIn$, sFileIn$, sTextOut$, sFileOut$
sFileIn = Get_FileToOpen(): If sFileIn = "" Then Exit Sub

sTextIn = ReadTextFile(sFileIn)
'Do something with sTextIn
sTextOut = DoThat(sTextIn)

'Overwrite the same file
WriteTextFile(sTextOut, sFileIn)
'-OR-
'Add to the same file
WriteTextFile(sTextOut, sFileIn, True)

'-OR- write to a different file
sFileOut = Get_FileToSave(): If sFileOut = "" Then Exit Sub
WriteTextFile(sTextOut, sFileOut)
'-OR- add to a different file
WriteTextFile(sTextOut, sFileOut, True)
End Sub


Function Get_FileToOpen$(Optional FileTypes$)
Dim vFile
If FileTypes = "" Then FileTypes = "All Files ""*.*"", *.*"
vFile = Application.GetOpenFileName(FileTypes)
Get_FileToOpen = IIf(vFile = False, "", vFile)
End Function

Function Get_FileToSave$(Optional FileOut$)
Dim vFile
vFile = Application.GetSaveAsFilename(FileOut)
Get_FileToSave = IIf(vFile = False, "", vFile)
End Function

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
  #7   Report Post  
Old May 10th 19, 10:29 AM posted to microsoft.public.excel.programming
external usenet poster
 
First recorded activity by ExcelBanter: May 2019
Posts: 22
Default Error message: "Bad file name or number"

As always, thank you Gary!


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
Error message "File format is not valid" MLee Excel Discussion (Misc queries) 2 August 1st 07 05:20 PM
"Unable to Read File" error message in Excel - solutions? Presby Loaner Excel Discussion (Misc queries) 2 June 27th 07 05:09 PM
How do I ignore the "Number stored as text" error message permane. reston33 Excel Discussion (Misc queries) 2 March 28th 07 02:59 AM
How to ged rid of the "Execution Error 9" message at the file opening? frenchbox Excel Discussion (Misc queries) 0 July 31st 06 03:28 PM
"file not found" error message when launching source file Lisa[_9_] Excel Programming 4 April 6th 04 02:54 PM


All times are GMT +1. The time now is 12:55 PM.

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

About Us

"It's about Microsoft Excel"

 

Copyright © 2017