2012-01-11 4 views
1

저는 Excel 2010 및 인터넷 탐색기 8 및 Vista에서 vba로 작업하고 있습니다. 아래 코드는 원격 웹 사이트로 이동하여 양식을 게시하는 작업입니다. 결과 페이지에서 코드는 '견적 가져 오기'버튼을 클릭해야합니다. 대신이 오류는 "개체 변수 또는 블록 변수가 설정되지 않았습니다"가 표시됩니다. 코드에서 강조 표시된 문제 행은 "estimate = ieApp.document.getElementById ("btnRequestEstimates ")"입니다.VBA 액세스 원격 웹 사이트, 다른 웹 페이지에서 이미 버튼을 클릭 한 후 자동으로 제출을 클릭하십시오.

나는 문제의 일부가 작동하지 않는 버튼 형태의 일부가 아닌 전송 버튼입니다 것 같아요. 또한 두 번째 버튼을 클릭하기 전에 변수를 재설정해야하는지 궁금합니다. 오류 메시지는이 문제가 자격 문제라는 것을 의미하지만이 상황에서 요소를 정규화하는 꽤 일반적인 방법이라고 생각합니다. 그것들은 내가 쓸데없이 인터넷 검색을 해왔다. 나는 그 문제가 무엇인지 잘 모른다.

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

"설정"이 없습니다. 제출은 양식 메서드이며 버튼에는 적용 할 수 없습니다. 당신은 아마 거기에 클릭 방법을 사용하고 싶어 ... –

답변

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 코드를 결합
관련 문제