locked
Error HRESULT E_FAIL has been returned from a call to a COM component

    Domanda

  • Help!

    I have a window that opens a word document from a databse and displays it to a user in an axWebBrowser control. This works fine the first time I open a document, but causes the above error every time after that. Restarting the program fixes it.

    Here is the error and code fragments of relevence:

    System.Reflection.TargetInvocationException was caught
      Message="Exception has been thrown by the target of an invocation."
      Source="mscorlib"
      StackTrace:
           at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
           at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
           at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
           at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
           at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
           at AxSHDocVw.AxWebBrowser.Navigate2(Object& uRL)
           at CLOD.net.frmWordViewer.frmWordViewer_Load(Object sender, EventArgs e) in C:\Documents and Settings\densmoreb.CTBS\My Documents\Visual Studio 2005\Projects\CFA\CLOD.net.root\CLOD.net\CLOD.net\UIL\frmWordViewer.vb:line 91

    Inner exception:
    Error HRESULT E_FAIL has been returned from a call to a COM component.

    Error code : -2147467259
    source: "Interop.SHDocVw"

    Code fragments:

    Public Class frmWordViewer
        Inherits System.Windows.Forms.Form

        Private _PATH As String = System.IO.Path.GetTempPath
        Private _strWordDoc As String
        Private _oDocument As Object

    ...
        Public Sub Init(ByVal strDoc As String)
            Me._strWordDoc = "file://" & strDoc
            Me._oDocument = Nothing
        End Sub

        Private Sub frmWordViewer_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            ' load the document before showing the form
            ' put the cursor in a wait - the loading may take a while
            ' NOTE: to self may need to invaldiate something or wrap the wait cursor somewhere else
            '       probably want to save the cursor coming in and restore after also
            Me._oDocument = Nothing
            Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
            Me.AxWebBrowser1.Parent = Me
            Try
                '            Me.AxWebBrowser1.Navigate(Me.DocName)
                Me.AxWebBrowser1.Navigate2(Me.DocName)
            Catch ex As Exception
                MsgBox("I think I'm going to crash!" & vbCrLf & "I couldn't open the document!" & vbCrLf & " Workaround = restart the program.", MsgBoxStyle.Critical, "Crash Remediation!")
                'HACK: AxWebBrowser in a bad state, calling proc better be in a try catch or we're going down in flames
            Finally
                Me.Cursor = System.Windows.Forms.Cursors.Default
            End Try

        End Sub


        Private Sub AxWebBrowser1_NavigateComplete2(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event) Handles AxWebBrowser1.NavigateComplete2
            ' Grab the document so we can manipulate it
            Me._oDocument = e.pDisp.Document
            With _oDocument.application.commandbars("Standard")
                .position = 4
                .Visible = True
            End With
            With _oDocument.application.commandbars("Formatting")
                .position = 4
                .Visible = True
            End With
            Me.AxWebBrowser1.ExecWB(SHDocVw.OLECMDID.OLECMDID_HIDETOOLBARS, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER)
        End Sub

        Private Sub frmWordViewer_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
            ' NOTES: Save a new document?
            '        grab any changes that need saving?
            '        do any other document manipulation here?
            Try
                ' close the document
                Me.AxWebBrowser1.ExecWB(SHDocVw.OLECMDID.OLECMDID_CLOSE, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER)
                Me._oDocument = Nothing
                ' delete the temporary file
            Catch ex As Exception
                '
            End Try

        End Sub

    ...
    mercoledì 6 settembre 2006 17:13

Risposte

  • I have still not been able to determine why closing the activex document causes the AxWebBrowser control to set an error internally. As I said it sets an error message inside itself, but doesn't trigger an exception. The activex command is inside of a try ... catch block.

    Here is the event sequence for the accessing of the AxWebBrowser:

    1) User selects a document from the "Document History Tree"
    2) User opens a popup menu and selects open
    3) This opens the frmWordViewer modal dialog
    a) frmWordViewer_Load called
    4) User does various things with the document
    5) User closes the frmWordViewer dialog
    a) frmWordViewer_FormClosed called

    I have hacked around this issue by destroying the AxWebBrowser and recreating it programmatically. This "fixes" the problem.

    Here's my hack in the form close event - which always happens between viewing documents:

    ...
    'HACK: DISPOSING AND RECREATING CONTROL FIXES THE PROBLEM
    Me.AxWebBrowser1.Dispose()
    Me.AxWebBrowser1 = New AxSHDocVw.AxWebBrowser
    CType(Me.AxWebBrowser1, System.ComponentModel.ISupportInitialize).BeginInit()
    Me.SuspendLayout()
    Me.AxWebBrowser1.Dock = System.Windows.Forms.DockStyle.Fill
    Me.AxWebBrowser1.Enabled = True
    Me.AxWebBrowser1.Location = New System.Drawing.Point(0, 0)
    Me.AxWebBrowser1.OcxState = CType(resources.GetObject("AxWebBrowser1.OcxState"), System.Windows.Forms.AxHost.State)
    Me.AxWebBrowser1.Size = New System.Drawing.Size(774, 489)
    Me.AxWebBrowser1.TabIndex = 5
    Me.Controls.Add(Me.AxWebBrowser1)
    CType(Me.AxWebBrowser1, System.ComponentModel.ISupportInitialize).EndInit()
    Me.ResumeLayout(False)
    'H_END
    ...




    venerdì 8 settembre 2006 15:14

Tutte le risposte

  • Not to sure asbout whats going on there it may be a problem with not disposing of the first instance before calling it again...but one thing for sure...you should use the .NET webbrowser control instead of the activeX control
    mercoledì 6 settembre 2006 17:56
  • DMan1,

    Thanks for the response, but I need to use the axWebBrowser control, so that I can load the toolbars and allow the users to edit the document and "save" it back via my code. I also have other "tasks" that I allow the users to do and handle the behavior programmatically. I need the ActiveX to pass commands through to Word in the background. This was the only control that I've found that allows me to interact with Word, in the ways I need to.

    I'm not sure what you mean by disposing of one instance before launching the next, can you elaborate?

    This is pretty much a new area for me. Also, not sure if this is relevent, but the Word document is in (Word) XML.
    mercoledì 6 settembre 2006 18:35
  • mmmm...interesting that you say you can not get the .net webrowser to work for ya...but any way...before I attempt to answer your question about the instance...can you point out which line of code causes the error?
    mercoledì 6 settembre 2006 18:51
  • DMan1,

    The offending code doesn't actually trigger an error, but any attermpt to access the axWebBrowser afterwards makes the error evident. The offending code is in red below;
    the ExecWB command. What I'm trying to do here is free up the file from the hidden word app. Basically it looks like this is invalidating the handle that axWebBrowser is using for the document. So, I think I need a way to reset the axWebBrowser control, or the ActiveX state.

    Private Sub frmWordViewer_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
    ' NOTES: Save a new document?
    ' grab any changes that need saving?
    ' do any other document manipulation here?
    Try
    ' close the document
    Me.AxWebBrowser1.ExecWB(SHDocVw.OLECMDID.OLECMDID_CLOSE, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER)
    Me._oDocument = Nothing
    ' delete the temporary file
    Catch ex As Exception
    '
    End Try

    End Sub


    mercoledì 6 settembre 2006 19:42
  • I have still not been able to determine why closing the activex document causes the AxWebBrowser control to set an error internally. As I said it sets an error message inside itself, but doesn't trigger an exception. The activex command is inside of a try ... catch block.

    Here is the event sequence for the accessing of the AxWebBrowser:

    1) User selects a document from the "Document History Tree"
    2) User opens a popup menu and selects open
    3) This opens the frmWordViewer modal dialog
    a) frmWordViewer_Load called
    4) User does various things with the document
    5) User closes the frmWordViewer dialog
    a) frmWordViewer_FormClosed called

    I have hacked around this issue by destroying the AxWebBrowser and recreating it programmatically. This "fixes" the problem.

    Here's my hack in the form close event - which always happens between viewing documents:

    ...
    'HACK: DISPOSING AND RECREATING CONTROL FIXES THE PROBLEM
    Me.AxWebBrowser1.Dispose()
    Me.AxWebBrowser1 = New AxSHDocVw.AxWebBrowser
    CType(Me.AxWebBrowser1, System.ComponentModel.ISupportInitialize).BeginInit()
    Me.SuspendLayout()
    Me.AxWebBrowser1.Dock = System.Windows.Forms.DockStyle.Fill
    Me.AxWebBrowser1.Enabled = True
    Me.AxWebBrowser1.Location = New System.Drawing.Point(0, 0)
    Me.AxWebBrowser1.OcxState = CType(resources.GetObject("AxWebBrowser1.OcxState"), System.Windows.Forms.AxHost.State)
    Me.AxWebBrowser1.Size = New System.Drawing.Size(774, 489)
    Me.AxWebBrowser1.TabIndex = 5
    Me.Controls.Add(Me.AxWebBrowser1)
    CType(Me.AxWebBrowser1, System.ComponentModel.ISupportInitialize).EndInit()
    Me.ResumeLayout(False)
    'H_END
    ...




    venerdì 8 settembre 2006 15:14