test file path for validity

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Below is a macro I've cobbled together by reading Help Files and posts on
these message boards, as well as asking questions on these boards - thanks to
everyone who has provided help to date.
Basically, it writes a file path stored in a text file into a textbox, and
then shows a userform.
Is it possible to test this line to see if it returns an error:
Open "C:\Documents and Settings\TJ\Desktop\ThrowAway\DefaultPath.txt"
without using On Error GoTo?
I know I've been on the wrong track so far - I always get Compile Error
Expected: Expression.

Sub LoadTest()
Dim yPath As String
Dim ft As Object
Dim DefaultFolder2 As Object

On Error GoTo OpenBlank

If MultiSave.txtFldrPath.Text = "" Then
Open "C:\Documents and
Settings\TJ\Desktop\ThrowAway\DefaultPath.txt" For Input As #1
Do While Not EOF(1)
Input #1, yPath
Loop
MultiSave.txtFldrPath.Text = yPath
End If


MultiSave.Show

Exit Sub

OpenBlank:
MultiSave.txtFldrPath.Text = ""
MultiSave.Show
End Sub


Note: When I wrote that I cobbled this together, I meant it. So, any
suggestions are welcome.
I'm trying to write the code for a form that will allow me to save a file
into three places at the same time. I am trying to store a default location
for each of the three places in text files stored on a network drive.

Thanks,
 
Hi tj,

To answer the first part of your question, you can use the Dir$()
function to test the validity of the path. You pass the path in as the
parameter, and check the returned value. If the path isn't valid, the
return will be an empty string.

Dim PathToTextFile As String
PathToTextFile = "C:\blah\blah"
If Dir$(PathToTextFile) <> "" Then
' the file exists, you can open it

The second part of the question, about suggestions, is really more
interesting. :-)

First, this code doesn't belong in a macro in a regular module. It
should be in the UserForm_Activate() procedure in the MultiSave
userform's code, which automatically runs when the userform is about
to be displayed. For this kind of project, only the declaration of the
userform, the .Show statement, and the destruction of the userform (by
setting it to Nothing) should be in a regular module.

Second, you don't need a loop to read the path from the text file.
Instead of the three lines
Do While Not EOF(1)
Input #1, yPath
Loop

all you need is the one line

Line Input #1, yPath

Third, after you read the line out of the text file, you have to have
the line

Close #1

Finally, instead of the On Error GoTo and the label, just use

On Error Resume Next
 
Thanks, Jay. This will help me along quite a bit.
Eventually, I hope to set a file level default directory using Variables, so
I'm sure I'll be back.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top