Problem closing word document from Word hosted in a ShDocVw contro

C

cycloptic

Hello all:

I am working on an Outlook Add-in using VB6. The add-in allows users to view
certain types of attachments along with a data entry form. The types of
documents include other MS Office files and PDF's.

I am using a web browser control, shdocvw.dll (vintage IE6), hosted on a VB6
form to view the attachments. Basically, the attachments are written to a
folder in a TEMP directory and are listed in a listbox so that users can pick
the attachment they wish to view. When they are done with the viewer, I
attempt to delete the files in the TEMP directory.

Initially, I was having a problem with deleting the files when viewing
Office documents because the instances of the Office application would not
close when navigating away from the documents they display and consequently
keep those files open.

From an internet search I found code that I use to obtain a reference to the
Application COM object for the document being viewed during the browser
control's NavigateComplete2 event as follows.

private oHost as Object

Private Sub DocViewer_NavigateComplete2(ByVal pDisp As Object, URL As Variant)
Dim doc As Variant
Dim ext As String

ext = GetFileExtension(CStr(URL))
If IsOfficeDocumentFile(ext) Then
Set doc = pDisp.Document
MsgBox "Opened by " & doc.Application.Name
Set oHost = doc.Application
Else
Set oHost = Nothing
End If

End Sub


I then attempt to use this to close the application when the user navigates
away from the document.


Private Sub DocsListbox_Click()
....
If Not (oHost Is Nothing) Then
oHost.Quit
Set oHost = Nothing
End If
....
End Sub


The above strategy seems to work for Excel and Powerpoint documents, but
with Word documents I get the following error message when the oHost.Quit
function is called:

Run-time Error 4605: This method or property is not available because this
document is in another application

There are no macros or add-ins in my Word environment so it is not a case of
Word attempting to run something external before quitting.

I also tried using

oHost.ActiveDocument.Close

instead of the Quit function, but I still get the same exact error. I've run
out of things to try at this point.

Has anyone figured out how to close Word, or at least close a document, when
Word is hosted by the Internet Explorer web browser control in a similar
situation? Does anyone see something that I may be doing incorrectly?

Thanks in advance for any responses.

cycloptic
 
C

cycloptic

Well, I may have found a solution.

Basically, when someone desires to close the viewer, instead of closing the
form directly, I set the embedded browser to navigate to "about:blank" and
set a flag. In the NavigateComplete2 event, I check the flag and close the
form at that point. So far, it seems that the browser has properly disposed
of any previously hosted word document by then and the temporary documents
can be deleted.

I guess my previous approach was cutting the browser off at the knees.

cycloptic
 

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