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

A

aehan

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
 
K

Karl E. Peterson

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.
 
A

aehan

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
 
K

Karl E. Peterson

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. :)
 
T

Tony Strazzeri

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.
 

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

Top