Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
aehan - 19 Dec 2007 18:13 GMT
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
Karl E. Peterson - 19 Dec 2007 21:32 GMT
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.
Signature

.NET: It's About Trust!
http://vfred.mvps.org

> Hi everyone
>
[quoted text clipped - 44 lines]
> Thanks for everything so far.
> Aehan
aehan - 20 Dec 2007 11:24 GMT
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

> 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
[quoted text clipped - 51 lines]
> > Thanks for everything so far.
> > Aehan
Karl E. Peterson - 20 Dec 2007 19:01 GMT
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.  :-)
Signature

.NET: It's About Trust!
http://vfred.mvps.org

> Hi Karl
>
[quoted text clipped - 66 lines]
>>> Thanks for everything so far.
>>> Aehan
Tony Strazzeri - 21 Dec 2007 00:09 GMT
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.

> 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
[quoted text clipped - 82 lines]
> >>> Thanks for everything so far.
> >>> Aehan
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.