WordApp not working while switching documents

DPrometheus

Well-known member
Joined
Jan 7, 2009
Messages
48
Location
The Netherlands
Hi there,

Ive encountered the following problem and Im really clueless.

For work Ive made a word host application, and now we need to switch templates in it. Ive made a custom ribbon where a combobox lists all templates. These templates are loaded in the combobox_change event. See code listing 1 below.

Ive a Word template (.dotm) called Spec.dotm. This template does nothing more then redirect to another template (Word07Def.dotm), which will be activated as some sort of global template (This enables me to switch documents). This global template contains the ribbon adjustments, and VBA code to handle the ribbon controls

If I do this in Word 2007 everything works fine, however if I do this in my host application (Which does nothing else then just open Spec.dotm, which succesfully redirects to Word07Def) it fails with error 91: object variable or with block not set. when I try to switch templates. If I check the Globally loaded templates I see Word07Def activated. Whenever it fails I see word flickering once or twice (which indicates it loads the other document, I guess) and then opening my previous document with the error code.

Weird thing is, if I do this in Word 2007 it works like a charm?! :confused:

I have word running in a panel control like code listing 2.

code 1:
Code:
Sub TypeBriefCombo_Change(control As IRibbonControl, text As String)
    On Error Resume Next
    
    Dim openFile As String
    openFile = ""
    
    Select Case text
    Case "Poliklinische brief"
         set filename string
        openFile = "K:\mso\AppInApp\Klinisch.docx"
        Templates("K:\mso\AppInApp\Klinisch.dotm").OpenAsDocument
    
    Case "Klinische brief"
        openFile = "K:\mso\AppInApp\Klinisch.docx"
    
    Case "Ontslagbrief"
        openFile = "K:\mso\AppInApp\Ontslag.docx"
    
    Case "Pati
 
Hi DPrometheus

Mmm. Im not sure Im following all the details of what youre doing, or why youre "switching" templates. You may need to describe that in a bit more detail.

As far as debugging goes - you can open a global template (as a document) while its still working as a global template. Then you should be able to see what lines causing the problem (and I very much recommend NOT using OnError Resume Next as this could run you through multiple errors, so youd have no idea what the original one might have been).
 
Hi Cindy,
thnx for replying.

Im working here at the local hospital. Im currently working on a control library for this particular hospital. For our former applications (VB6) they have built text editors for each module, we now want a more robust way to implement text editors. Since every employee is familiar with Word, and its highly customizable, we choose to create a word panel. This sits in our control library, and will open Word07 within its client rectangle. If we load a template with customized ribbons and functionality we can make one single control which can be used for each specialisation within the hospital. Which buttons and addins are available are controlled by templates. These can be switched by the user (for users with different occupations, or specialisations which closely overlap) and by the application itself. Those documents listed above in my first post were just for testing purposes. Available documents are shown based on logged in user or state of the application.

Furthermore I think Word.ActiveDocument equals to nothing when I load from my .net application and the real problem exists in the following snippet:
Code:
 If FileExists(openFile) Then
         Activate new template
        Word.ActiveDocument.Close (False)
        Word.Documents.Open(openFile).Activate
    End If

if I insert something like:
Code:
    If Word.ActiveDocument = "" Then
        MsgBox "Word ActiveDocument doesnt exists in code!"
    End If
I get no messages in my application (which uses the Word-panel) and in Word 2007 itself. However, Word 2007 keeps the newly loaded document, while the applications switches back to the old one. And when I have the On Error Resume Next statement it also displays the error number and description posted above.
Last I removed the On Error Resume Next and check for error values after ActiveDocument.Close and Documents.Open

it displays two messages (One error for each function :( ) loosely translated into something similar like this:
Error 4198: Command failed. (this goes for the close function)
I found a similar tread for this one microsoft newsgroups but without a real solution.
followed with error 91 as shown above (first thread, on open function)

Any ideas why the applications both run different code paths with the same source code? Word 2007 has no issues with my templates while the panel keeps spawning errors.
Why does the document fails to close? It really gives a descriptive error for that line.

I hope I made myself clear.

thanks in advance
~ DP
 
Last edited by a moderator:
Hi DPrometheus

Lets start with the thing Im absolutely sure about:

Last I removed the On Error Resume Next and it displays a message loosely translated into something similar like this:
Error 13 during execution. Types dont match.
on line : If Not Word.ActiveDocument Then

When you use "IF NOT" youre asking about a boolean, but ActiveDocument is an object, not a value. Try something more like:

If Word.ActiveDocument Is Nothing Then

That means no object could be assigned to ActiveDocument (it doesnt exist).

This should, at least, get rid of the error messages you mention.

The only time I would expect to not have an ActiveDocument object is if there are no documents open. A safer way to test this might be
If wdApp.Documents.Count < 1 Then

Also, at a "professional" level, Id try very hard to not work with the ActiveDocument object. My inclination would be to keep a collection of some kind of all documents in a particular instance. Monitor the DocumentOpen, NewDocument, DocumentClose and perhaps DocumentChange events so that my code always "knows" which document object (if any) is the current one. That would complicate things considerably, but the approach could help workaround some issues.
 
thanks for your quick response.

I changed the if not word.ActiveDocument to if word.ActiveDocument is nothing. Just before you posted I already changed it to if word.ActiveDocument = "" which also got me rid of the error.

Close happens to trigger with the right ActiveDocument, (document1, even Word07 and WordApp points to the same document before they shut it down) but still it fails to close that one, exiting with error 4198 - command fails, while running this from the application.

Im about to implement the document collection as Im back from my break, see if that does any good.

thanks for your suggestions.

~DP

EDIT:
I found the bug. Within the .net environment I changed some handlers for the document events. (DocumentOpen, NewDocument & DocumentBeforeClose) Within the DocumentBeforeClose event I was canceling out that same event, since it could close the panel as well. After changing this the document succesfully switches from template to template. I just have to trust the end user not pressing the closing-X while in the editor. I made a small warning messagebox just in case.

Thanks for your time and suggestions
~DP
 
Last edited by a moderator:
Back
Top