2012-01-11 10 views
1

私はvbaとExcel 2010とInternet Explorer 8とVistaで作業しています。以下のコードは、リモートWebサイトにアクセスしてフォームを投稿する作業です。表示されるページで、コードは「見積もりを取得」ボタンをクリックします。代わりに、私はこのエラー "オブジェクト変数またはブロック変数が設定されていない"を取得します。コードの強調表示された問題の行は、 "estimate = ieApp.document.getElementById(" btnRequestEstimates ")"です。VBAアクセスのリモートWebサイトは、他のWebページ上のボタンをクリックして自動化ボタンをクリックすると自動投稿

私は、問題の一部は、動作していないボタンがフォームの一部ではない送信ボタンである可能性があると考えています。また、2番目のボタンをクリックする前に変数をリセットする必要があるかどうかも疑問です。エラーメッセージはこれが資格問題であることを意味しますが、私はそれがこの状況で要素を修飾するかなり標準的な方法だと思います。それらは私が無駄にグーグルしているいくつかのものです、私は本当に問題が何であるかは分かりません。

Sub btn_version() 

Dim ieApp As Object 
Dim ieDoc As Object 
Dim ieForm As Object 
Dim ieObj As Object 
Dim URL As String 
Dim estimate As Object 

URL = "http://www.craft-e-corner.com/p-2688-new-testament-cricut-cartridge.aspx" 
Set ieApp = CreateObject("InternetExplorer.Application") 
ieApp.Visible = True 
ieApp.navigate URL 
While ieApp.Busy Or ieApp.readyState <> 4: DoEvents: Wend 

Set ieDoc = ieApp.document 
Set ieForm = ieDoc.forms(1) 
For Each ieObj In ieForm.Elements 
If ieObj.ClassName = "AddToCartButton" Then 
ieObj.Click 
End If 
Next ieObj 

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
While ieApp.Busy Or ieApp.readyState <> 4: DoEvents: Wend 
estimate = ieApp.document.getElementById("btnRequestEstimates") 
estimate.submit 
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 

End Sub 
+0

"セット"がありません。 submitはフォームメソッドであり、ボタンには適用されません。あなたはおそらくそこのクリックメソッドを使用したいと思う... –

答えて

3

以下のコードは、this page

Sub Scrape2() 

Dim objIE As Object 
Dim xmlhttp As Object 
Dim ieButton As Object 
Dim strResponse As String 
Dim strUrl As String 

strUrl = "http://www.craft-e-corner.com/addtocart.aspx?returnurl=showproduct.aspx%3fProductID%3d2688%26SEName%3dnew-testament-cricut-cartridge" 
Set objIE = CreateObject("InternetExplorer.Application") 
objIE.navigate "about:blank" 
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP") 

'~~> Indicates that page that will receive the request and the type of request being submitted 
xmlhttp.Open "POST", "http://www.craft-e-corner.com/addtocart.aspx?returnurl=showproduct.aspx%3fProductID%3d2688%26SEName%3dnew-testament-cricut-cartridge", False 
'~~> Indicate that the body of the request contains form data 
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" 
'~~> Send the data as name/value pairs 
xmlhttp.Send "Quantity=1&VariantID=2705&ProductID=2688" 
strResponse = xmlhttp.responseText 
objIE.navigate strUrl 
objIE.Visible = True 

Do While objIE.readystate <> 4 
    DoEvents 
Loop 

objIE.document.Write strResponse 

Set xmlhttp = Nothing 
Set ieButton = objIE.document.getelementbyid("btnRequestEstimates") 
ieButton.Click 

End Sub 
から、最終的なURLに "btnRequestEstimatesボタンをクリックすると(私たちの問題のあなたの Set ieDoc = ieApp.documentセクションを飛ばし、つまり、あなたの POSTのよりよい制御を与えるために) automate submitting a post form that is on a website with vba and xmlhttpからあなたXMLHTTPコードを組み合わせました
関連する問題