Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi there,

 

I've encountered the following problem and I'm really clueless.

 

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

 

I've 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:

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ëntenbrief"
       openFile = "K:\mso\AppInApp\Ontslag.docx"

   End Select
   
   If FileExists(openFile) Then
       ' Activate new template
       Word.ActiveDocument.Close (False)
       Word.Documents.Open(openFile).Activate
   End If
   
   If Err.Number <> 0 Then
       MsgBox "Err: " & Err.Number & " " & Err.Description
   End If
End Sub

 

code 2

       ...
       ' Use Opusapp for word applications
       If _wordWnd = 0 Then _wordWnd = FindWindow("Opusapp", Nothing)
       If _wordWnd <> 0 Then
           SetParent(_wordWnd, Me.Handle.ToInt32)
           ...
           _wordApp.Visible = True
           
           SetWindowPos(_wordWnd, Me.Handle.ToInt32(), 0, 0, Me.Bounds.Width, Me.Bounds.Height, SWP_NOZORDER Or SWP_NOMOVE Or SWP_DRAWFRAME Or SWP_NOSIZE)

           OnResize()
           ...

 

As a final note, since the Word07Def.dotm is loaded via Spec.dotm Word07 is not able to open the project Word07Def in VBE, so I can't set any real breakpoints.

 

Someone got any idea's or tips?

 

~ DP

My Development System

Intel Core i7 920 @2.66Ghz

6 GB DDR3 SDRAM

Windows 7 Ultimate x64 & Windows Vista Home Premium x64 dual boot

GeForce GTX295 1.8 GB

3.5 TB HD

Posted

Hi DPrometheus

 

Mmm. I'm not sure I'm following all the details of what you're doing, or why you're "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 it's still working as a global template. Then you should be able to see what line's causing the problem (and I very much recommend NOT using OnError Resume Next as this could run you through multiple errors, so you'd have no idea what the original one might have been).

Posted (edited)

Hi Cindy,

thnx for replying.

 

I'm working here at the local hospital. I'm 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 it's 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:

If FileExists(openFile) Then
       ' Activate new template
       Word.ActiveDocument.Close (False)
       Word.Documents.Open(openFile).Activate
   End If

 

if I insert something like:

   If Word.ActiveDocument = "" Then
       MsgBox "Word ActiveDocument doesn't 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 idea's 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

Edited by DPrometheus

My Development System

Intel Core i7 920 @2.66Ghz

6 GB DDR3 SDRAM

Windows 7 Ultimate x64 & Windows Vista Home Premium x64 dual boot

GeForce GTX295 1.8 GB

3.5 TB HD

Posted

Hi DPrometheus

 

Let's start with the thing I'm 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 don't match.

on line : If Not Word.ActiveDocument Then

 

When you use "IF NOT" you're 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 doesn't 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, I'd 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.

Posted (edited)

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.

 

I'm about to implement the document collection as I'm 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

Edited by DPrometheus

My Development System

Intel Core i7 920 @2.66Ghz

6 GB DDR3 SDRAM

Windows 7 Ultimate x64 & Windows Vista Home Premium x64 dual boot

GeForce GTX295 1.8 GB

3.5 TB HD

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...