Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
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
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
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
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
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 | |
|
|
Similar Threads | ||||
Thread | Forum | |||
How to remove a path to load a file I've moved under the File menu | New Users | |||
What is the maximum number of characters you can use to name a fil | Microsoft Word Help | |||
How to creat relative and shorthand file path names? | Microsoft Word Help | |||
How to create relative and shorthand file path names | Microsoft Word Help | |||
file name and path footer? | Microsoft Word Help |