PDA

View Full Version : Runtime 91 error on userform populating one input box on webpage



1819
03-09-2017, 08:03 PM
I simply need to use VBA to replicate the manual process of entering data into an input box on a webpage and clicking return.

The code I have is below. It is returning a runtime error 91 at the line shown.



Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim url As String
Dim lastRow As Long

url = "https://twitrss.me/"

'~~> This is the sheet where the values has to be picked up from
Set ws = Sheets("RSSTwitMe")

With ws
'~~> Get the Last Row in Sheet1
lastRow = .Range("A" & .Rows.Count).End(xlUp).Row

'~~> Loop through the range
For i = 1 To lastRow
'~~> Navigate to the URL
WebBrowser1.Navigate url
WaitForWBReady

'~~> Input Values
WebBrowser1.Document.getElementById("screen_name").Value = .Range("A" & i).Value

'~~> Click Button
WebBrowser1.Document.getElementsByTagname("fetch")(2).Click '<<<<<<<<<<<<<<<<RUNTIME ERROR 91

WaitForWBReady
Next
End With
End Sub

Private Sub Wait(ByVal nSec As Long)
nSec = nSec + Timer
While Timer < nSec
DoEvents
Sleep 100
Wend
End Sub

Private Sub WaitForWBReady()
Wait 1
While WebBrowser1.ReadyState <> 4
Wait 3
Wend
End Sub


I understand that this is usually caused because the VBA code does not match the code of the web page.

So I have also included below the code from the website showing the relevant section in HTML



<div class="container">
<h1>TwitRSS.me</h1>
<h2>Get your twitter user feed as RSS</h2>
<p> </p>
<form method="get" action="/twitter_user_to_rss/">
<label for="screen_name">Twitter User:</label>
<br>
@
<input id="screen_name" class="input" type="text" placeholder="ciderpunx" value="" size="20" maxlength="140" name="user">
<br>
<label for="replies">With replies?:</label>
<input id="replies" type="checkbox" style="margin-left:1.2em" name="replies">
<br>
<label class="hid" for="submit"> </label>
<input id="fetch" class="btn btn-primary btn-lg" type="submit" value=" Fetch RSS ">
</form>
<p></p>
<h2 style="padding-top:2em">
<form method="get" action="/twitter_search_to_rss/">
<p></p>
</div>
</div>
<div class="container">
<div class="container-alternate">
<footer>
<script src="//code.jquery.com/jquery-1.10.2.min.js">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.2/js/bootstrap.min.js">
<script>
<script type="text/javascript">
<div class="igtranslator-main-div" style="display: none; width: 0px; height: 0px;">
<div class="igtranslator-activator-icon bounceIn" style="background-image: url("resource://jid1-dgnibwqga0sibw-at-jetpack/data/icons/home.png"); display: none;" title="Click to Show Translation"></div>
<div class="SnapLinksContainer" style="margin-left: 0px; margin-top: 0px; display: none;">
</body>


Finally, I will try to attach a small sample worksheet but at present VBA Express is not working properly to accept it.

Please help make this work. Many thanks.

mancubus
03-10-2017, 12:07 AM
check this out:

https://groups.google.com/forum/#!topic/excelvbamacros/8TCrzNJG054

1819
03-10-2017, 06:33 AM
Thanks for this link - it will be really useful - but I am still looking for a direct answer to my problem in the original post.

The macro I am trying to fix allows tweets to be collected in an RSS feeder and then stored or used for analysis.

So it's no about transmitting tweets.

Thanks again.

keebellah
03-10-2017, 06:40 AM
The message is "Object Variable not set" I suggest you step through the code and check the line where it happens

mancubus
03-10-2017, 12:32 PM
adopt below code.
worked for me.



Sub vbax_58828_click_button_web_page()

Dim v_elements, v_ele

With CreateObject("InternetExplorer.application")
.Visible = True
.Navigate "https://twitrss.me/"

Do Until .ReadyState = 4
DoEvents
Loop

.Document.getElementById("screen_name").Value = "TwitterUserNameHere"

Set v_elements = .Document.getElementsByTagName("input")
For Each v_ele In v_elements
If v_ele.getAttribute("value") = " Fetch RSS " Then
v_ele.Click
Exit For
End If
Next v_ele

Do Until .ReadyState = 4
DoEvents
Loop
End With

End Sub

keebellah
03-10-2017, 01:16 PM
@mancubus: worked for me too, nice one

1819
03-11-2017, 09:52 AM
adopt below code.
worked for me.



Sub vbax_58828_click_button_web_page()

Dim v_elements, v_ele

With CreateObject("InternetExplorer.application")
.Visible = True
.Navigate "https://twitrss.me/"

Do Until .ReadyState = 4
DoEvents
Loop

.Document.getElementById("screen_name").Value = "TwitterUserNameHere"

Set v_elements = .Document.getElementsByTagName("input")
For Each v_ele In v_elements
If v_ele.getAttribute("value") = " Fetch RSS " Then
v_ele.Click
Exit For
End If
Next v_ele

Do Until .ReadyState = 4
DoEvents
Loop
End With

End Sub


Thanks @mancubus for this - really helpful. I've just found that the macro needs to use Firefox because the information inputted is used by a Firefox extension. How would the code adapt to work in Firefox? Thanks.

mancubus
03-12-2017, 10:19 AM
you are welcome.

pls dont quote the previous messages and codes as a whole. refer to the parts where necessary.

i am not sure if this can be done via Firefox or Chrome.

this may give you an ides.

http://www.makeuseof.com/tag/how-to-automate-firefox-or-chrome-with-vba-and-selenium/