VB6 and MsWord in a Webbrowser Control

Hi Guys

In the following function:
Private Sub wb_NavigateComplete2(ByVal pDisp As Object, url As Variant)
   
   'On Error Resume Next
   Set oDocument = pDisp.Document

   MsgBox "File opened by: " & oDocument.Application.Name
   
   If (pDisp Is wb.Object) Then
            'Debug.Print "wb_NavigateComplete2 = " & Now() & " - " & url
            'MsgBox "Complete - " & url
            bDoneDownloading = True
    End If

   ' hide tools bars
   wb.ExecWB OLECMDID_HIDETOOLBARS, OLECMDEXECOPT_DONTPROMPTUSER
End Sub

Open in new window


when I unload the browser control, it fails on this line:

MsgBox "File opened by: " & oDocument.Application.Name

with an error 438 saying object doesnt support this property or method.

To avoid the use of "on error resume next", how would I fail gracefully.

I have tried navigating away from the word document by implementing a function
Public Sub UnloadWord()
   wb.Navigate2 "about:blank"  ' So that webbrowser starts in 'msWord mode' and empty document
End Sub

Open in new window


which is called in the form unload event

MTIA

DWE
LVL 1
dwe0608Asked:
Who is Participating?
 
dwe0608Connect With a Mentor Author Commented:
ok - well heres where I am at - a bit of research and I found a few bits ...

Private Sub wb_NavigateComplete2(ByVal pDisp As Object, url As Variant)
   
'    MsgBox CStr(url)
    If IsOfficeDocumentFile(CStr(url)) Then
        Set oDocument = pDisp.Document
    '    MsgBox "Opened by " & oDocument.Application.Name
        Set oHost = oDocument.Application
        wb.ExecWB OLECMDID_HIDETOOLBARS, OLECMDEXECOPT_DONTPROMPTUSER
    Else
        Set oHost = Nothing
    End If
   
   If (pDisp Is wb.Object) Then
            'Debug.Print "wb_NavigateComplete2 = " & Now() & " - " & url
            'MsgBox "Complete - " & url
            bDoneDownloading = True
   End If
   
End Sub

Open in new window


Helper Functions
Private Function GetFileExtension(ByVal FileName As String) As String
    Dim i As Long
    For i = Len(FileName) To 1 Step -1
        Select Case Mid$(FileName, i, 1)
            Case "."
                GetFileExtension = Mid$(FileName, i + 1)
                Exit For
            Case ":", "\"
                Exit For
        End Select
    Next
End Function

Private Function IsOfficeDocumentFile(str As String) As Boolean
  Dim ext$
  ext = GetFileExtension(str)
  Select Case LCase(ext)
    Case "rtf"
        IsOfficeDocument = True
    Case "doc"
        IsOfficeDocument = True
    Case "docx"
        IsOfficeDocument = True
    Case Else
         IsOfficeDocument = False
  End Select
End Function

Open in new window


Basically, what I do is test the document (the URL parameter) of NavigateComplete2 - if its a word extension, then I have an active word document and I can deal with that - if its not, I ignore the pDisb parameter for the large part, save to test and make sure it contains a valid object and if it does, set a variable letting me know that the Navigation has completed.

Can anyone confirm that my code is correct ? And if not, tidy it up?

MTIA

DWE
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
relying on the file extension is not the safest way, but shall work in most practical cases.

other technique would be to check if oDocument is nothing, and if not, then check oDocument.Application (if it's nothing or not), and finally use oDocument.Application.Name aso..
0
 
dwe0608Author Commented:
no expert assistance in arriving at the answer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.