Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

VB6 and MsWord in a Webbrowser Control

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

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

808 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