Solved

VB6 and MsWord in a Webbrowser Control

Posted on 2014-03-27
3
332 Views
Last Modified: 2014-04-06
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
0
Comment
Question by:dwe0608
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
dwe0608 earned 0 total points
ID: 39958314
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39963636
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
 
LVL 1

Author Closing Comment

by:dwe0608
ID: 39981027
no expert assistance in arriving at the answer
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question