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

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
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,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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