Customising an error message

D

dozingquinn

Hello,

I'm using the following code to open a word document saved on a network
drive

Sub label105_click
Set Word = CreateObject("Word.Application")
Word.Visible = TRUE
Word.Documents.open("G:\whatever.dot")
End Sub

I've been assured that almost everyone in the company has access to the
G:\ - however I'd like to make a message box appear if Word can't
access the document - something like "Please contact IT to map your
G:\"

Can I just alter the code so it states....

Sub label105_click
Set Word = CreateObject("Word.Application")
Word.Visible = TRUE
Word.Documents.open("G:\whatever.dot")
If Word.Documents.open Is Nothing Then
MsgBox "Please contact IT to map your G:\"
Else
End Sub
 
S

Sue Mosher [MVP-Outlook]

That's close, but you need to explicitly return the Document object from the Open method:

On Error Resume Next
Set doc = Word.Documents.open("G:\whatever.dot")
If doc Is Nothing Then
MsgBox "Please contact IT to map your G:\"
Else
' do stuff with doc
End If


--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
H

Hollis Paul

Sub label105_click
Set Word = CreateObject("Word.Application")
Word.Visible = TRUE
Word.Documents.open("G:\whatever.dot")
If Word.Documents.open Is Nothing Then
MsgBox "Please contact IT to map your G:\"
Else
End Sub
If I read the code naively, then the last open on "Word.Documents.open"
is a method, not an object. So, this line should throw an error to the
effect that no document is found. And really, you have created a world
of confusion for yourself by naming your Word Application Object
"Word". You should be using naming practices to make clear what the
objects are in lines of code removed from where you create them.

So, you should use something line the following for the first object:

Set myWordApp = CreateObject("Word.Application")

and you should test that object to see if you created it.

Then, if Word were structured like Outlook, and it really isn't, you
would have to create a Documents collection object. But, in looking
quickly through my last custom form using Word Documents, I find where
the document object is created, and the next thing I see is where a
template is added:

myWordApp.Documents.Add strWordTemplate
myWordApp.Visible = True
myWordDoc = myWordApp.ActiveDocument

In any case, I think you need to find an example of working with Word
documents through Outlook, set up a VBA project, set references to the
several Word object models, and get creative with the Object Browser
(F2) to find the names of the objects you need to use, and the name of
the methods you will have to use, and find out what each does.

This is no easy task for a hard day's night's burning of the midnight
oil. It will probably take several weeks. Be sure and tell your
supervisor so time will be allocated for the task.

I think you can find a relevant code example on www.outlookcode.com .
I found my starter example on an old, old web-site, and I would not
recommend anyone else go that route.
 
D

dozingquinn

Thanks for your reply Hollis. I'm a bit of a newbie when it comes to VB
code. As I'm successfully using the basic code without the error
message box, I thought it would be an easy case of adding something
like "if there's an error in opening the document, then display this
message" however it all looks a bit more complicated than I imagined.

If there isn't a simple 'if error, then msgbox' code fix then I think
I'll leave the code alone.

Thanks again.
 
D

dozingquinn

Thanks Sue,

I'm now using the following code:

Sub label105_click
Set Word = CreateObject("Word.Application")
Word.Visible = TRUE
Word.Documents.open("G:\whatever.dot")
On Error Resume Next
Set doc = Word.Documents.open("G:\whatever.dot")
If doc Is Nothing Then
MsgBox "Please contact IT to map your G:\"
Else
End If

I trust that it will work (My testing is limited as I can't upload the
form to the public drive - and our IT department get a bit narky with
me when I keep asking them to re-upload the form).
 
S

Sue Mosher [MVP-Outlook]

You'll want to put that On Error Resume Next as the first line in the Sub.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 

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