Computer Forum Forum Index
Register  FAQ Profile Log in to check your private messages Log in Register 

Need to check if a drive is there if not ignore it

 
Post new topic   Reply to topic    Computer Forum Forum Index -> microsoft word vba general
View previous topic :: View next topic  
Author Message
aehan
Guest





PostPosted: Wed Dec 19, 2007 6:13 pm    Post subject: Need to check if a drive is there if not ignore it Reply with quote

Hi everyone

I had a problem where I was placing a custom menu in Word which opens
documents based on templates. Jay Freedman was super, and really helped me
on it, however it's still giving me problems, so I think I must have
explained what I wanted to do wrongly.

The code as it is now is as Jay suggested:

Sub OpenLetter()
Dim strNetworkPath As String
Dim strCPath As String
Dim strFile As String

strNetworkPath = "G:\Documents and Settings\" & Environ("USERNAME") &
"\Application Data\Microsoft\Templates\My Templates"
strCPath = Environ("USERPROFILE") & "\Application
Data\Microsoft\Templates\My Templates"

' Opens document based on letter template

If Dir(strNetworkPath, vbDirectory) <> "" Then

strFile = strNetworkPath & "\Letter.dot"
Else
strFile = strCPath & "\Letter.dot"
End If

On Error GoTo BadTemplate
Documents.Add Template:=strFile, NewTemplate:=False, DocumentType:=0

Exit Sub

BadTemplate:
MsgBox "Couldn't find " & strFile
End Sub

If the user is working with a computer that has a network drive on it there
is no problem at all, but if they are working on a PC that only has a C
drive, they get an error because the code seems to need to see a network
drive - at least it appears that way. If I comment out all the code that
refers to the network, the routine works and finds the template on the C
drive, so the code is all fine, except that I need it to say that if it
cannot find a network drive, then find the template in the C drive. I hope
that makes sense, and that you can tell me what I need to do.

Thanks for everything so far.
Aehan
Back to top
Google
Sponsor





PostPosted: Wed Dec 19, 2007 6:13 pm    Post subject: Advertisement

Back to top
Karl E. Peterson
Guest





PostPosted: Wed Dec 19, 2007 9:32 pm    Post subject: Re: Need to check if a drive is there if not ignore it Reply with quote

You need to become more familiar with standard debugging techniques. Put your
cursor in that routine, perhaps on the If statement, and press F9 to set a
breakpoint. Now execute your code. When it gets to that breakpoint, execution will
stop, and you'll be debugging. Press F8 to step a single line of code. You can
inspect the values of your variables simply by holding the cursor over them, or
opening the Locals window (on the View menu). It should become obvious what the
problem is, or at least what line is causing it.
--
..NET: It's About Trust!
http://vfred.mvps.org



aehan wrote:
Quote:
Hi everyone

I had a problem where I was placing a custom menu in Word which opens
documents based on templates. Jay Freedman was super, and really helped me
on it, however it's still giving me problems, so I think I must have
explained what I wanted to do wrongly.

The code as it is now is as Jay suggested:

Sub OpenLetter()
Dim strNetworkPath As String
Dim strCPath As String
Dim strFile As String

strNetworkPath = "G:\Documents and Settings\" & Environ("USERNAME") &
"\Application Data\Microsoft\Templates\My Templates"
strCPath = Environ("USERPROFILE") & "\Application
Data\Microsoft\Templates\My Templates"

' Opens document based on letter template

If Dir(strNetworkPath, vbDirectory) <> "" Then

strFile = strNetworkPath & "\Letter.dot"
Else
strFile = strCPath & "\Letter.dot"
End If

On Error GoTo BadTemplate
Documents.Add Template:=strFile, NewTemplate:=False, DocumentType:=0

Exit Sub

BadTemplate:
MsgBox "Couldn't find " & strFile
End Sub

If the user is working with a computer that has a network drive on it there
is no problem at all, but if they are working on a PC that only has a C
drive, they get an error because the code seems to need to see a network
drive - at least it appears that way. If I comment out all the code that
refers to the network, the routine works and finds the template on the C
drive, so the code is all fine, except that I need it to say that if it
cannot find a network drive, then find the template in the C drive. I hope
that makes sense, and that you can tell me what I need to do.

Thanks for everything so far.
Aehan
Back to top
aehan
Guest





PostPosted: Thu Dec 20, 2007 11:24 am    Post subject: Re: Need to check if a drive is there if not ignore it Reply with quote

Hi Karl

Thanks for the advice. Actually, I woke up this morning and something just
clicked - I've sorted out the problem just by turning the code around. I did
try debugging, that's what told me that the code needed to find a network
drive before it could go on. So I've made it look for the C drive first and
it works!

Happy Christmas
Aehan

"Karl E. Peterson" wrote:

Quote:
You need to become more familiar with standard debugging techniques. Put your
cursor in that routine, perhaps on the If statement, and press F9 to set a
breakpoint. Now execute your code. When it gets to that breakpoint, execution will
stop, and you'll be debugging. Press F8 to step a single line of code. You can
inspect the values of your variables simply by holding the cursor over them, or
opening the Locals window (on the View menu). It should become obvious what the
problem is, or at least what line is causing it.
--
..NET: It's About Trust!
http://vfred.mvps.org



aehan wrote:
Hi everyone

I had a problem where I was placing a custom menu in Word which opens
documents based on templates. Jay Freedman was super, and really helped me
on it, however it's still giving me problems, so I think I must have
explained what I wanted to do wrongly.

The code as it is now is as Jay suggested:

Sub OpenLetter()
Dim strNetworkPath As String
Dim strCPath As String
Dim strFile As String

strNetworkPath = "G:\Documents and Settings\" & Environ("USERNAME") &
"\Application Data\Microsoft\Templates\My Templates"
strCPath = Environ("USERPROFILE") & "\Application
Data\Microsoft\Templates\My Templates"

' Opens document based on letter template

If Dir(strNetworkPath, vbDirectory) <> "" Then

strFile = strNetworkPath & "\Letter.dot"
Else
strFile = strCPath & "\Letter.dot"
End If

On Error GoTo BadTemplate
Documents.Add Template:=strFile, NewTemplate:=False, DocumentType:=0

Exit Sub

BadTemplate:
MsgBox "Couldn't find " & strFile
End Sub

If the user is working with a computer that has a network drive on it there
is no problem at all, but if they are working on a PC that only has a C
drive, they get an error because the code seems to need to see a network
drive - at least it appears that way. If I comment out all the code that
refers to the network, the routine works and finds the template on the C
drive, so the code is all fine, except that I need it to say that if it
cannot find a network drive, then find the template in the C drive. I hope
that makes sense, and that you can tell me what I need to do.

Thanks for everything so far.
Aehan



Back to top
Karl E. Peterson
Guest





PostPosted: Thu Dec 20, 2007 7:01 pm    Post subject: Re: Need to check if a drive is there if not ignore it Reply with quote

Well, I'm glad it's working for you, but that doesn't actually sound like a
solution. Just something that hid the problem. I mean, odds are, if the network is
there that's where the template oughta be loaded from, right?

More likely, the "error" was that the user had a workgroup folder set for templates,
or something like that, rather than the hardcoded default you're using.

But that's really irrelevent. You don't know why the original was failing. You're
just ignoring the failure, waving your hands around a bit, and trying to divert
attention while you pull a sleight of hand trick. I stick by my original advice,
and suggest you become more familiar with debugging techniques and try to develop a
more healthy sense of curiosity. Smile
--
..NET: It's About Trust!
http://vfred.mvps.org



aehan wrote:
Quote:
Hi Karl

Thanks for the advice. Actually, I woke up this morning and something just
clicked - I've sorted out the problem just by turning the code around. I did
try debugging, that's what told me that the code needed to find a network
drive before it could go on. So I've made it look for the C drive first and
it works!

Happy Christmas
Aehan

"Karl E. Peterson" wrote:

You need to become more familiar with standard debugging techniques. Put your
cursor in that routine, perhaps on the If statement, and press F9 to set a
breakpoint. Now execute your code. When it gets to that breakpoint, execution
will stop, and you'll be debugging. Press F8 to step a single line of code.
You can inspect the values of your variables simply by holding the cursor over
them, or opening the Locals window (on the View menu). It should become obvious
what the problem is, or at least what line is causing it.
--
..NET: It's About Trust!
http://vfred.mvps.org



aehan wrote:
Hi everyone

I had a problem where I was placing a custom menu in Word which opens
documents based on templates. Jay Freedman was super, and really helped me
on it, however it's still giving me problems, so I think I must have
explained what I wanted to do wrongly.

The code as it is now is as Jay suggested:

Sub OpenLetter()
Dim strNetworkPath As String
Dim strCPath As String
Dim strFile As String

strNetworkPath = "G:\Documents and Settings\" & Environ("USERNAME") &
"\Application Data\Microsoft\Templates\My Templates"
strCPath = Environ("USERPROFILE") & "\Application
Data\Microsoft\Templates\My Templates"

' Opens document based on letter template

If Dir(strNetworkPath, vbDirectory) <> "" Then

strFile = strNetworkPath & "\Letter.dot"
Else
strFile = strCPath & "\Letter.dot"
End If

On Error GoTo BadTemplate
Documents.Add Template:=strFile, NewTemplate:=False, DocumentType:=0

Exit Sub

BadTemplate:
MsgBox "Couldn't find " & strFile
End Sub

If the user is working with a computer that has a network drive on it there
is no problem at all, but if they are working on a PC that only has a C
drive, they get an error because the code seems to need to see a network
drive - at least it appears that way. If I comment out all the code that
refers to the network, the routine works and finds the template on the C
drive, so the code is all fine, except that I need it to say that if it
cannot find a network drive, then find the template in the C drive. I hope
that makes sense, and that you can tell me what I need to do.

Thanks for everything so far.
Aehan
Back to top
Tony Strazzeri
Guest





PostPosted: Fri Dec 21, 2007 12:09 am    Post subject: Re: Need to check if a drive is there if not ignore it Reply with quote

I agree with Karl on the need to actually find and fix the problem.

By swapping the order around you will never access the network drive
so why the question in the first place?

There are several ways to check for the existence of a drive or folder
in a drive have a look at the FSO (File System Object) FolderExists
Method

Looking at your code however I notice that you are not checking for
the existence of thefile you are trying to open. You are only
checking for the existence of the folder it is in. That could be your
problem.

Cheers
TonyS.


On Dec 21, 6:01 am, "Karl E. Peterson" <k...@mvps.org> wrote:
Quote:
Well, I'm glad it's working for you, but that doesn't actually sound like a
solution. Just something that hid the problem. I mean, odds are, if the network is
there that's where the template oughta be loaded from, right?

More likely, the "error" was that the user had a workgroup folder set for templates,
or something like that, rather than the hardcoded default you're using.

But that's really irrelevent. You don't know why the original was failing. You're
just ignoring the failure, waving your hands around a bit, and trying to divert
attention while you pull a sleight of hand trick. I stick by my original advice,
and suggest you become more familiar with debugging techniques and try to develop a
more healthy sense of curiosity. Smile
--
.NET: It's About Trust!
http://vfred.mvps.org

aehan wrote:
Hi Karl

Thanks for the advice. Actually, I woke up this morning and something just
clicked - I've sorted out the problem just by turning the code around. I did
try debugging, that's what told me that the code needed to find a network
drive before it could go on. So I've made it look for the C drive first and
it works!

Happy Christmas
Aehan

"Karl E. Peterson" wrote:

You need to become more familiar with standard debugging techniques. Put your
cursor in that routine, perhaps on the If statement, and press F9 to set a
breakpoint. Now execute your code. When it gets to that breakpoint, execution
will stop, and you'll be debugging. Press F8 to step a single line of code.
You can inspect the values of your variables simply by holding the cursor over
them, or opening the Locals window (on the View menu). It should become obvious
what the problem is, or at least what line is causing it.
--
..NET: It's About Trust!
http://vfred.mvps.org

aehan wrote:
Hi everyone

I had a problem where I was placing a custom menu in Word which opens
documents based on templates. Jay Freedman was super, and really helped me
on it, however it's still giving me problems, so I think I must have
explained what I wanted to do wrongly.

The code as it is now is as Jay suggested:

Sub OpenLetter()
Dim strNetworkPath As String
Dim strCPath As String
Dim strFile As String

strNetworkPath = "G:\Documents and Settings\" & Environ("USERNAME") &
"\Application Data\Microsoft\Templates\My Templates"
strCPath = Environ("USERPROFILE") & "\Application
Data\Microsoft\Templates\My Templates"

' Opens document based on letter template

If Dir(strNetworkPath, vbDirectory) <> "" Then

strFile = strNetworkPath & "\Letter.dot"
Else
strFile = strCPath & "\Letter.dot"
End If

On Error GoTo BadTemplate
Documents.Add Template:=strFile, NewTemplate:=False, DocumentType:=0

Exit Sub

BadTemplate:
MsgBox "Couldn't find " & strFile
End Sub

If the user is working with a computer that has a network drive on it there
is no problem at all, but if they are working on a PC that only has a C
drive, they get an error because the code seems to need to see a network
drive - at least it appears that way. If I comment out all the code that
refers to the network, the routine works and finds the template on the C
drive, so the code is all fine, except that I need it to say that if it
cannot find a network drive, then find the template in the C drive. I hope
that makes sense, and that you can tell me what I need to do.

Thanks for everything so far.
Aehan
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Computer Forum Forum Index -> microsoft word vba general All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Computer Forum