Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - False IE doc body error - "Object reference not set to an instance of an object"

Reply
 
Old 07-13-2006   #1 (permalink)
Alex K. Angelopoulos [MVP]


 
 

False IE doc body error - "Object reference not set to an instance of an object"

I go through the process of getting a legitimate reference to Internet
Explorer, but then can't get a legitimate document body reference for
automatic display. Why not? It appears that I really AM getting an object
reference.

Here's an example.

PS> $ie = New-Object -ComObject InternetExplorer.Application
PS> $ie.Navigate("http://www.google.com")
PS> $ie.Visible = $true
PS> $ie.document.body
format-default : Object reference not set to an instance of an object.

Let's be more direct. The following ends up giving us an immense member
listing:

PS> $body = $ie.document.body
PS> $body | gm
TypeName: mshtml.HTMLBodyClass

Name ...

However, if we try to just show the body, we still get the spurious error
message:

PS> $body
format-default : Object reference not set to an instance of an object.

Note that we can see properties of $body as well; for example,
$body.innerText shows us the actual page display text.



My System SpecsSystem Spec
Old 07-13-2006   #2 (permalink)
Yuksel Akinci [MSFT]


 
 

Re: False IE doc body error - "Object reference not set to an instance of an object"

The problem is specific to this COM object, PowerShell can't enumerate the
following collections.

filters
IHTMLElement_filters


--
Yuksel Akinci [MSFT]
Windows PowerShell Team
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.

Visit the Windows PowerShell Team blog at:
http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:
http://www.microsoft.com/technet/scr.../hubs/msh.mspx
"Alex K. Angelopoulos [MVP]" <aka@online.mvps.org> wrote in message
news:O1PPFTspGHA.2256@TK2MSFTNGP03.phx.gbl...
>I go through the process of getting a legitimate reference to Internet
>Explorer, but then can't get a legitimate document body reference for
>automatic display. Why not? It appears that I really AM getting an object
>reference.
>
> Here's an example.
>
> PS> $ie = New-Object -ComObject InternetExplorer.Application
> PS> $ie.Navigate("http://www.google.com")
> PS> $ie.Visible = $true
> PS> $ie.document.body
> format-default : Object reference not set to an instance of an object.
>
> Let's be more direct. The following ends up giving us an immense member
> listing:
>
> PS> $body = $ie.document.body
> PS> $body | gm
> TypeName: mshtml.HTMLBodyClass
>
> Name ...
>
> However, if we try to just show the body, we still get the spurious error
> message:
>
> PS> $body
> format-default : Object reference not set to an instance of an object.
>
> Note that we can see properties of $body as well; for example,
> $body.innerText shows us the actual page display text.
>
>



My System SpecsSystem Spec
Old 07-13-2006   #3 (permalink)
Greg Borota


 
 

Re: False IE doc body error - "Object reference not set to an instance of an object"

I also wanted to play with IE from PowerShell. Bumped into so many bumps
that I have to choose a different language for that.

COM support is problematic in VBScript as well. I tried to cancel navigation
from BeforeNavigate2 Event by setting Cancel to true, but that would never
work. It behaves as if in VBScript, COM parameters are always ByVal even if
they are defined as OUT params in COM. I asked Eric Lippert about that, but
never got anything back from him on this. Documentation seem to never
mention this aspect.

Greg

"Alex K. Angelopoulos [MVP]" <> wrote in message
news:O1PPFTspGHA.2256@TK2MSFTNGP03.phx.gbl...
>I go through the process of getting a legitimate reference to Internet
>Explorer, but then can't get a legitimate document body reference for
>automatic display. Why not? It appears that I really AM getting an object
>reference.
>
> Here's an example.
>
> PS> $ie = New-Object -ComObject InternetExplorer.Application
> PS> $ie.Navigate("http://www.google.com")
> PS> $ie.Visible = $true
> PS> $ie.document.body
> format-default : Object reference not set to an instance of an object.
>
> Let's be more direct. The following ends up giving us an immense member
> listing:
>
> PS> $body = $ie.document.body
> PS> $body | gm
> TypeName: mshtml.HTMLBodyClass
>
> Name ...
>
> However, if we try to just show the body, we still get the spurious error
> message:
>
> PS> $body
> format-default : Object reference not set to an instance of an object.
>
> Note that we can see properties of $body as well; for example,
> $body.innerText shows us the actual page display text.
>
>



My System SpecsSystem Spec
Old 07-14-2006   #4 (permalink)
Alex K. Angelopoulos [MVP]


 
 

Re: False IE doc body error - "Object reference not set to an instance of an object"

Are you familiar with the technical issues behind this problem? That's what
I'm really interested in. We have the same problem with MANY different
method calls, most commonly involving enumerations and HTML docs.
Unfortunately, they tend to be show-stoppers for many truly useful
automation activities.

"Yuksel Akinci [MSFT]" <yuksela@microsoft.com> wrote in message
news:OlUxDAtpGHA.3324@TK2MSFTNGP05.phx.gbl...
> The problem is specific to this COM object, PowerShell can't enumerate the
> following collections.
>
> filters
> IHTMLElement_filters
>
>
> --
> Yuksel Akinci [MSFT]
> Windows PowerShell Team
> Microsoft Corporation
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>
> Visit the Windows PowerShell Team blog at:
> http://blogs.msdn.com/PowerShell
> Visit the Windows PowerShell ScriptCenter at:
> http://www.microsoft.com/technet/scr.../hubs/msh.mspx
> "Alex K. Angelopoulos [MVP]" <aka@online.mvps.org> wrote in message
> news:O1PPFTspGHA.2256@TK2MSFTNGP03.phx.gbl...
>>I go through the process of getting a legitimate reference to Internet
>>Explorer, but then can't get a legitimate document body reference for
>>automatic display. Why not? It appears that I really AM getting an object
>>reference.
>>
>> Here's an example.
>>
>> PS> $ie = New-Object -ComObject InternetExplorer.Application
>> PS> $ie.Navigate("http://www.google.com")
>> PS> $ie.Visible = $true
>> PS> $ie.document.body
>> format-default : Object reference not set to an instance of an object.
>>
>> Let's be more direct. The following ends up giving us an immense member
>> listing:
>>
>> PS> $body = $ie.document.body
>> PS> $body | gm
>> TypeName: mshtml.HTMLBodyClass
>>
>> Name ...
>>
>> However, if we try to just show the body, we still get the spurious error
>> message:
>>
>> PS> $body
>> format-default : Object reference not set to an instance of an object.
>>
>> Note that we can see properties of $body as well; for example,
>> $body.innerText shows us the actual page display text.
>>
>>

>
>



My System SpecsSystem Spec
Old 07-14-2006   #5 (permalink)
Alex K. Angelopoulos [MVP]


 
 

Re: False IE doc body error - "Object reference not set to an instance of an object"

It's a pain to do because of the weird compatibility rules with VBScript
byref calls that vary depending on number of args, arrayness, and consuming
a return value, but I've pretty much always been able to get byref values
back.

The sad thing is that PowerShell really DID work nicely with IE in 2004 and
early 2005.

"Greg Borota" <borota@newsgroups.microsoft> wrote in message
news:u%23uSFutpGHA.1600@TK2MSFTNGP04.phx.gbl...
>I also wanted to play with IE from PowerShell. Bumped into so many bumps
>that I have to choose a different language for that.
>
> COM support is problematic in VBScript as well. I tried to cancel
> navigation from BeforeNavigate2 Event by setting Cancel to true, but that
> would never work. It behaves as if in VBScript, COM parameters are always
> ByVal even if they are defined as OUT params in COM. I asked Eric Lippert
> about that, but never got anything back from him on this. Documentation
> seem to never mention this aspect.
>
> Greg
>
> "Alex K. Angelopoulos [MVP]" <> wrote in message
> news:O1PPFTspGHA.2256@TK2MSFTNGP03.phx.gbl...
>>I go through the process of getting a legitimate reference to Internet
>>Explorer, but then can't get a legitimate document body reference for
>>automatic display. Why not? It appears that I really AM getting an object
>>reference.
>>
>> Here's an example.
>>
>> PS> $ie = New-Object -ComObject InternetExplorer.Application
>> PS> $ie.Navigate("http://www.google.com")
>> PS> $ie.Visible = $true
>> PS> $ie.document.body
>> format-default : Object reference not set to an instance of an object.
>>
>> Let's be more direct. The following ends up giving us an immense member
>> listing:
>>
>> PS> $body = $ie.document.body
>> PS> $body | gm
>> TypeName: mshtml.HTMLBodyClass
>>
>> Name ...
>>
>> However, if we try to just show the body, we still get the spurious error
>> message:
>>
>> PS> $body
>> format-default : Object reference not set to an instance of an object.
>>
>> Note that we can see properties of $body as well; for example,
>> $body.innerText shows us the actual page display text.
>>
>>

>
>



My System SpecsSystem Spec
Old 07-14-2006   #6 (permalink)
=?Utf-8?B?R3JlZyBCb3JvdGE=?=


 
 

Re: False IE doc body error - "Object reference not set to an inst

I thought that is was a general rule - no ByRef from COM in VBScript. Thanks
for confirming the opposite. But still this doesn't hold for BeforeNavigate2.
I've seen other people on the web complaining about this too (but no
solution) - setting Cancel to true in your BeforeNavigate2 handler has no
effect - the navigation goes ahead undisturbed. But that's OT for this list,
so I'll stop here

Greg

"Alex K. Angelopoulos [MVP]" wrote:

> It's a pain to do because of the weird compatibility rules with VBScript
> byref calls that vary depending on number of args, arrayness, and consuming
> a return value, but I've pretty much always been able to get byref values
> back.
>
> The sad thing is that PowerShell really DID work nicely with IE in 2004 and
> early 2005.
>
> "Greg Borota" <borota@newsgroups.microsoft> wrote in message
> news:u%23uSFutpGHA.1600@TK2MSFTNGP04.phx.gbl...
> >I also wanted to play with IE from PowerShell. Bumped into so many bumps
> >that I have to choose a different language for that.
> >
> > COM support is problematic in VBScript as well. I tried to cancel
> > navigation from BeforeNavigate2 Event by setting Cancel to true, but that
> > would never work. It behaves as if in VBScript, COM parameters are always
> > ByVal even if they are defined as OUT params in COM. I asked Eric Lippert
> > about that, but never got anything back from him on this. Documentation
> > seem to never mention this aspect.
> >
> > Greg
> >
> > "Alex K. Angelopoulos [MVP]" <> wrote in message
> > news:O1PPFTspGHA.2256@TK2MSFTNGP03.phx.gbl...
> >>I go through the process of getting a legitimate reference to Internet
> >>Explorer, but then can't get a legitimate document body reference for
> >>automatic display. Why not? It appears that I really AM getting an object
> >>reference.
> >>
> >> Here's an example.
> >>
> >> PS> $ie = New-Object -ComObject InternetExplorer.Application
> >> PS> $ie.Navigate("http://www.google.com")
> >> PS> $ie.Visible = $true
> >> PS> $ie.document.body
> >> format-default : Object reference not set to an instance of an object.
> >>
> >> Let's be more direct. The following ends up giving us an immense member
> >> listing:
> >>
> >> PS> $body = $ie.document.body
> >> PS> $body | gm
> >> TypeName: mshtml.HTMLBodyClass
> >>
> >> Name ...
> >>
> >> However, if we try to just show the body, we still get the spurious error
> >> message:
> >>
> >> PS> $body
> >> format-default : Object reference not set to an instance of an object.
> >>
> >> Note that we can see properties of $body as well; for example,
> >> $body.innerText shows us the actual page display text.
> >>
> >>

> >
> >

>
>
>

My System SpecsSystem Spec
Old 07-14-2006   #7 (permalink)
Michael Harris \(MVP\)


 
 

Re: False IE doc body error - "Object reference not set to an inst

Greg Borota wrote:
> I thought that is was a general rule - no ByRef from COM in VBScript.
> Thanks for confirming the opposite. But still this doesn't hold for
> BeforeNavigate2. I've seen other people on the web complaining about
> this too (but no solution) - setting Cancel to true in your
> BeforeNavigate2 handler has no effect - the navigation goes ahead
> undisturbed. But that's OT for this list, so I'll stop here


A workable BeforeNavigate2 event handler needs to support the bCancel
argument as a strongly typed Boolean ByRef and VBScript only supports
variants. The OLE automation layer effectively coerces the Boolean ByRef to
Variant ByVal. A client language like classic VB/VBA handles the bCancel
argument just fine, just not VBScript.

--
Michael Harris
Microsoft MVP Scripting


My System SpecsSystem Spec
Old 07-15-2006   #8 (permalink)
=?Utf-8?B?R3JlZyBCb3JvdGE=?=


 
 

Re: False IE doc body error - "Object reference not set to an inst

Thanks for shining the light on this.

"Michael Harris (MVP)" wrote:

> Greg Borota wrote:
> > I thought that is was a general rule - no ByRef from COM in VBScript.
> > Thanks for confirming the opposite. But still this doesn't hold for
> > BeforeNavigate2. I've seen other people on the web complaining about
> > this too (but no solution) - setting Cancel to true in your
> > BeforeNavigate2 handler has no effect - the navigation goes ahead
> > undisturbed. But that's OT for this list, so I'll stop here

>
> A workable BeforeNavigate2 event handler needs to support the bCancel
> argument as a strongly typed Boolean ByRef and VBScript only supports
> variants. The OLE automation layer effectively coerces the Boolean ByRef to
> Variant ByVal. A client language like classic VB/VBA handles the bCancel
> argument just fine, just not VBScript.
>
> --
> Michael Harris
> Microsoft MVP Scripting
>
>
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Inherit from usercontrol - Object not set to instance of an object .NET General
runtime error: "Object required" VB Script
datalist -- Object reference not set to an instance of an object. .NET General
Error: "Cannot convert "System.Object[]" to "System.Int32"." PowerShell
Writing a "good" error object? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46