Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.docmanagement
tjtjjtjt
 
Posts: n/a
Default test file path for validity

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,
--
tj
  #2   Report Post  
Posted to microsoft.public.word.docmanagement
Jay Freedman
 
Posts: n/a
Default test file path for validity

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

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org

On Wed, 16 Nov 2005 18:10:02 -0800, "tjtjjtjt"
wrote:

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,

  #3   Report Post  
Posted to microsoft.public.word.docmanagement
tjtjjtjt
 
Posts: n/a
Default test file path for validity

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.

--
tj


"Jay Freedman" wrote:

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

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org

On Wed, 16 Nov 2005 18:10:02 -0800, "tjtjjtjt"
wrote:

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,


Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to remove a path to load a file I've moved under the File menu Donaldo New Users 15 August 25th 05 10:14 AM
What is the maximum number of characters you can use to name a fil nilufer Microsoft Word Help 3 June 15th 05 11:14 AM
How to creat relative and shorthand file path names? 2dogs Microsoft Word Help 1 May 15th 05 12:11 PM
How to create relative and shorthand file path names 2dogs Microsoft Word Help 4 May 14th 05 08:49 PM
file name and path footer? Legal Learning Microsoft Word Help 0 January 20th 05 04:25 PM


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

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"