Solved

VB6 and MsWord in a Webbrowser Control

Posted on 2014-03-27
3
330 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now