[RESOLVED] "Run time error '52': Bad file name or number" when i open associated text file
i am getting error "Run time error '52': Bad file name or number" when i use this code to open a text file that is accociated with my app.
File association is set up using inno.
when i replace Command$ with "C:\Users\Dave\Documents\dave.cppl" and run it in design mode there is no error.
Code:
Private Sub Form_Load()
Dim DoubleClickedFile As String
Dim sFileText As String
Dim iFileNo As Integer
DoubleClickedFile = Command$
If DoubleClickedFile <> "" Then
DoubleClickedFile = Mid(DoubleClickedFile, 4, Len(DoubleClickedFile) - 3)
Text1.Text = DoubleClickedFile 'to check if the file and path is good
iFileNo = FreeFile
Open DoubleClickedFile For Input As #iFileNo
Input #iFileNo, sFileText
Text1.Text = sFileText
Close #iFileNo
End If
End Sub
Re: "Run time error '52': Bad file name or number" when i open associated text file
What does Command$ return?
Place break pointer right on that line and you will see the actual value.
Or you can at least print value to debug (Debug.Print DoubleClickedFile).
Re: "Run time error '52': Bad file name or number" when i open associated text file
DoubleClickedFile = Mid(DoubleClickedFile, 4, Len(DoubleClickedFile) - 3)
returns this: \Users\Dave\Documents\dave.cppl"
and DoubleClickedFile returns this: "C:\Users\Dave\Documents\dave.cppl"
i was trying to remove the drive letter but did nit remove the right side quote marks.
Does the drive letter need to be removed of this to work?
Re: "Run time error '52': Bad file name or number" when i open associated text file
Why are you trying to remove the drive letter? :confused:
That is an important part of the file location, and as you are opening the file you want to know the full file location.
Re: "Run time error '52': Bad file name or number" when i open associated text file
I saw the same problem posted elswhere in google and the solution was to remove the drive letter. but this does not work.
Re: "Run time error '52': Bad file name or number" when i open associated text file
Path must be valid (or complete) or you will get an error.
Solution from that Google search could've related to a different problem though.
Re: "Run time error '52': Bad file name or number" when i open associated text file
I can't think of any situation like this where removing the drive letter would be a good thing.
Does Command$ actually contain the " character at the start and end?
If so, this should solve it:
Code:
DoubleClickedFile = Command$
If DoubleClickedFile <> "" Then
If Left$(DoubleClickedFile, 1) = """" Then DoubleClickedFile = Mid$(DoubleClickedFile, Len(DoubleClickedFile) -2)
Text1.Text = DoubleClickedFile 'to check if the file and path is good
iFileNo = FreeFile
...
Re: "Run time error '52': Bad file name or number" when i open associated text file
this code returns this: "C:\Users\Dave\Documents\dave.cppl" in text1.text
Code:
Private Sub Form_Load()
On Error Resume Next
Dim DoubleClickedFile As String
Dim sFileText As String
Dim iFileNo As Integer
DoubleClickedFile = Command$
If DoubleClickedFile <> "" Then
'If Left$(DoubleClickedFile, 1) = """" Then DoubleClickedFile = Mid$(DoubleClickedFile, Len(DoubleClickedFile) - 2)
Text1.Text = DoubleClickedFile
iFileNo = FreeFile
Open DoubleClickedFile For Input As #iFileNo
Input #iFileNo, sFileText
Text2.Text = sFileText
Close #iFileNo
End If
End Sub
and this code returns this: pl"
Code:
Private Sub Form_Load()
On Error Resume Next
Dim DoubleClickedFile As String
Dim sFileText As String
Dim iFileNo As Integer
DoubleClickedFile = Command$
If DoubleClickedFile <> "" Then
If Left$(DoubleClickedFile, 1) = """" Then DoubleClickedFile = Mid$(DoubleClickedFile, Len(DoubleClickedFile) - 2)
Text1.Text = DoubleClickedFile
iFileNo = FreeFile
Open DoubleClickedFile For Input As #iFileNo
Input #iFileNo, sFileText
Text2.Text = sFileText
Close #iFileNo
End If
End Sub
either way i get "Run time error '52': Bad file name or number" when i remove "on error resume next"
Re: "Run time error '52': Bad file name or number" when i open associated text file
Ooops, I missed a parameter:
Code:
If Left$(DoubleClickedFile, 1) = """" Then DoubleClickedFile = Mid$(DoubleClickedFile, 2, Len(DoubleClickedFile) -2)
Re: "Run time error '52': Bad file name or number" when i open associated text file
so my problem was quotes around the file and path.
Thanks, this works.