Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Handle WORD.APPLICATION errors in an ACCESS.APPLICATION 1

Status
Not open for further replies.

Axoliien

Programmer
Aug 19, 2003
166
US
This is a rather complex and technical question and I would guess only a few may have had prior experience with such issues. If you have done application design between different MS Office programs and have ever handled errors between the different programs, I would appreciate your help.

I am trying to do a mail merge project using MS Access to pipe data into an MS Word mail merge template. Everything is working great and we are having no problems, but I am a stickler about writing error handling routines so I am trying to handle errors that may occur in either Access or the Word app I reference. Here is an example of the problem.

Inside of an [red]MS Access[/red] program we build the mail merge properties on a referenced [purple]WORD.APPLICATION[/purple] as [blue]WApp[/blue]. Let's say that the [blue]WApp[/blue] tries to open a file but cannot find the file. [blue]WApp[/blue] generates an error that we normally handle within the [purple]Word Application[/purple], which in this case propogates an error raise into [red]MS Access[/red]. Though an error is raised in [red]MS Access[/red], our [red]Access[/red] program only knows is that an error occurred. [red]MS Access[/red] has no error number or description, since the native error occurred in the [purple]Word Application[/purple]. The question is, how can I get the error from [blue]WApp[/blue] so that I can handle such occurrences in the [red]Access[/red] program?
 
Just a though.
What about the LastDLLError and Source properties of the Err object ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I undestand what you mean when you speak of the LastDLLError and the Source properties, however this will not allow me to handle the specific errors occurring. With the above example, I receive a LastDLLError = 997, Number = 0, Descr = "", and Source = "" so I can only tell that something happened in MS Word, which is exactly what I already know. If I am missing something, please fill me in.
 
Axaliien,

Once I had a consultant in my group helping me with a project and I remember this issue coming up.

LastDLLError will only return an Error number; you need to link that number to a message.
To get the associated error message you need the Microsoft Platform SDK.

Once you download the right SDK for your OS look into the FORMATMESSAGE function and the GetLastError function.
This should point you in the right direction.


NICE
 
You shouldn't have to deal with the LastDLLError unless you're using API calls directly. If you're just using COM to automate msword the automation code will bubble the error back to Access without a problem.

VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
I would like to know how you create instances of MS Word through MS Access and are able to handle the Word errors, VBSlammer, that is exactly what I am looking for. A simple example that shows how you are able to do this would suffice. Here is how I am doing things

Code:
[blue]Public Sub[/blue] SetWordObject ([blue]ByRef[/blue] WApp [blue]As[/blue] Word.Application)
[blue]On Error Resume Next[/blue]
    [green]'Get an open word app if there is one.[/green]
    [blue]Set[/blue] WApp = GetObject(, "Word.Application")
    [blue]If[/blue] Err.Number = 429 [blue]Then[/blue] [green]'No open word app, create a new instance.[/green]
        [blue]Set[/blue] WApp = [blue]New[/blue] Word.Application
        Err = 0
    [/blue]End If[/blue]
[blue]On Error GoTo[/blue] WordAppErr
    [green]'Do something here that will cause an error in Word, not Access.[/green]
    WApp.Documents.Open "C:\IncorrectFile.doc"
    [blue]Exit Sub[/blue]

WordAppErr:
    [green]'Need to handle Word errors here.  Output of Err objects does not work.[/green]
    Debug.Print Err.Number [green]'Prints 0[/green]
    Debug.Print Err.Description [green]'Prints empty string[/green]
    Debug.Print Err.LastDllError [green]'Prints 997[/green]
[blue]End Sub[/blue]

If I run this code, what ends up happening is that Access opens a new instance of Word and then tries to open the non-existant file. I receive an error in Access that says there was an error in Word, however no information about the error is available except that the last DLL was 997. If you can include your working code that would be a great benefit.

Thanks!
 
I tested your code as is using the following method:
Code:
Public Sub TestWordErr()

  Dim wd As Word.Application
  Call SetWordObject(wd)
  
End Sub
and the output to the debug window was:
Code:
 5174 
This file could not be found.
Try one or more of the following:
* Check the spelling of the name of the document.
* Try a different file name.
 (C:\IncorrectFile.doc)
 0

VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
Wow, I must have something missing in my Access References, I don't get any of that information that you receive. What are your references so I may update mine?
 
Actually, I think you might have a hardware problem or something. You might want to run scandisk to see if you have a disk error.

VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
I am wondering if we are on the same level here. Let me start over with what I am asking of you VBSlammer.

When I run this:
Code:
Public Sub TestWordErr()

  Dim wd As Word.Application
  Call SetWordObject(wd)
  
End Sub

Public Sub SetWordObject (ByRef WApp As Word.Application)
On Error Resume Next
    'Get an open word app if there is one.
    Set WApp = GetObject(, "Word.Application")
    If Err.Number = 429 Then 'No open word app, create a new instance.
        Set WApp = New Word.Application
        Err = 0
    [/blue]End If[/blue]
On Error GoTo WordAppErr
    'Do something here that will cause an error in Word, not Access.
    WApp.Documents.Open "C:\IncorrectFile.doc"
    Set WApp = Nothing
    Exit Sub

WordAppErr:
    'Need to handle Word errors here.  Output of Err objects does not work.
    Debug.Print "Number: " & Err.Number
    Debug.Print "Description: " & Err.Description
    Debug.Print "LastDLLError: " & Err.LastDllError
End Sub

I receive the following in my debug window.
Code:
Number: 0
Description: 
LastDLLError: 997

The error number is 0, which suggests the operation completed successfully as given by your link listing system error messages. Just to help out, the Err.Number and the Err.LastDllError are not the same thing. As defined by Visual Basic Documentation, the DLL error is specific to the DLL being called and does not reference the system error codes.

You and I are seeing 2 completely different things, so I would first like to find out why. I am trying to get the error number 5174 as you did, as that is what I am asking for.

I thank you for your help all!
 
Well, I am getting the error message now. I gave up on the access database and simply created a new one, imported all of my tables and code into the new one, and it works fine. That's Microsoft for you I guess!

Thanks for your help, had it been working in the first place I wouldn't have had to bother you all with it. I did see something I wanted to learn a bit more about with those system error messages though, I appreciate it.

Thanks,
Axoliien
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top