GetModuleByTabIndex(i).HTMLDocumentObj throws exception

Add-in Express™ Support Service
That's what is more important than anything else

GetModuleByTabIndex(i).HTMLDocumentObj throws exception
 
Vadim Usmanov


Guest


Hi.
I'm trying to fill in a field on a web page searching for tab url .
This code works well.

       Dim MyField
        Dim i
        For i = 0 To GetModuleCount() - 1 
            Dim MyModule As IEModule = TryCast(Me.GetModuleByTabIndex(i), IEModule)
            If MyModule IsNot Nothing Then
                Dim TabDomain = MyModule.HTMLDocumentObj.domain
                If TabDomain = "translate.google.ru" Then 
                    MyField = MyModule.HTMLDocumentObj.getElementById("source")
                    If (MyField IsNot Nothing) Then 
                        MyField.Value = "Test" 
                    End If
                    MyModule.TabActivate() 
                    Exit Sub
                End If
            End If
Next


But sometimes when I close and open a new tab(s) an exception throws in the line with MyModule.HTMLDocumentObj.domain:

Exception Type: System.NullReferenceException
Exception Message: Object reference not set to an instance of an object.

or

Exception Type: System.NotSupportedException
Exception Message: Exception HRESULT: 0x800A01B6
Posted 31 Aug, 2013 04:55:38 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi Vadim,

Please try the code below:



    Public Function GetDomain() As String
        If Not (HTMLDocumentObj Is Nothing) Then
            Return HTMLDocumentObj.domain
        End If
        Return String.Empty
    End Function

    Public Function GetElement(id As String) As Object
        If Not (HTMLDocumentObj Is Nothing) Then
            Return HTMLDocumentObj.getElementById(id)
        End If
        Return String.Empty
    End Function

        Dim MyField
        Dim i

        For i = 0 To GetModuleCount() - 1
            Dim MyModule As IEModule = TryCast(Me.GetModuleByTabIndex(i), IEModule)
            If MyModule IsNot Nothing Then
                Dim TabDomain = MyModule.GetDomain()
                If TabDomain = "translate.google.ru" Then
                    MyField = MyModule.GetElement("source")
                    If (MyField IsNot Nothing) Then
                        MyField.Value = "Test"
                    End If
                    MyModule.TabActivate()
                    Exit Sub
                End If
            End If
        Next

Posted 02 Sep, 2013 07:37:54 Top
Vadim Usmanov


Guest


Thank you Sergey. But unfortunately the problem is not solved...
Posted 02 Sep, 2013 11:49:26 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi Vadim,

What the error do you get now? Please use the Visual Studio Debugger to learn what the code produces the error. Also, you need to check the value of the Me.IEApp.ReadyState property before you obtain the domain. It should be 'Interop.SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE'.
Posted 03 Sep, 2013 10:11:49 Top
Vadim Usmanov


Guest


Thanks Sergey. I'll check.
Posted 05 Sep, 2013 08:03:53 Top