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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
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,215,377
Messages
6,124,598
Members
449,174
Latest member
chandan4057

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