Unable to Solve this error - Method Document of object iwebbrowser2 failed

AnilPullagura

Board Regular
Joined
Nov 19, 2010
Messages
98
Hello All,

I am trying to create a macro that opens a website that is windows authenticated and click on a button named "Login" and then take a screen shot and paste it in a new word document.
I am getting the error, "Method Document of object iwebbrowser2 failed" and when I click on Debug and execute, the macro executes. I want to get rid of the error and have not been successful so far.

Please help me as where the code is incorrect.


Option Explicit
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal _
bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Const VK_SNAPSHOT = &H2C
Global Const SW_MAXIMIZE = 3
Global Const SW_SHOWNORMAL = 1
Global Const SW_SHOWMINIMIZED = 2

Sub PrintScreen()
Dim IE As Object
Dim elems As Object
Dim e As Variant
Dim objCollection As Object

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "http://example.com/"
With IE.document
Set objCollection = IE.document.getElementsByTagName("input")
Set elems = .getElementsByTagName("input")
For Each e In elems
If (e.getAttribute("value") = "Login") Then
e.Click
Exit For
End If
Next e
End With
keybd_event VK_SNAPSHOT, 1, 0, 0
Dim MW As Object
Set MW = CreateObject("Word.Application")
MW.Visible = True
MW.Activate
MW.Documents.Add
'MW.WindowState = wdWindowStateMaximize
MW.Selection.Paste
'keybd_event VK_SNAPSHOT, 1, 0, 0
'Activeword.Paste
End Sub

Thanks,
Anil
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
I think you probably need to wait until the page has loaded. Here's one possible solution but there are others:

Code:
Option Explicit
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal _
bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Const VK_SNAPSHOT = &H2C
Global Const SW_MAXIMIZE = 3
Global Const SW_SHOWNORMAL = 1
Global Const SW_SHOWMINIMIZED = 2
Sub PrintScreen()

Dim IE As Object
Dim elems As Object
Dim e As Variant
Dim objCollection As Object
Dim docComplete As Boolean

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "http://example.com/"

docComplete = False
On Error Resume Next
Do Until docComplete
    docComplete = IE.document.ReadyState = "complete"
Loop
On Error GoTo 0

With IE.document
    Set objCollection = IE.document.getElementsByTagName("input")
    Set elems = .getElementsByTagName("input")
    For Each e In elems
        If (e.getAttribute("value") = "Login") Then
            e.Click
            Exit For
        End If
    Next e
End With

keybd_event VK_SNAPSHOT, 1, 0, 0

Dim MW As Object
Set MW = CreateObject("Word.Application")
MW.Visible = True
MW.Activate
MW.Documents.Add
'MW.WindowState = wdWindowStateMaximize
MW.Selection.Paste
'keybd_event VK_SNAPSHOT, 1, 0, 0
'Activeword.Paste

End Sub

WBD
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top