2013-02-21 239 views
2

我正在寫一個excel宏來填寫網站上的表單。我已經編寫了可以輕鬆填充文本框的代碼,並且找到了代碼來選擇廣播框,但是我在下拉菜單中選擇信息時遇到了問題。例如。性別。 組合框有三個選項。選擇/男性/女性。使用excel vba更改網站上的下拉菜單的值

伊夫試圖對這個一些變化: doc.getElementsByName( 「xs_r_gender」)項目(0)。價值= 「男性」

但沒有運氣。 這是網頁源代碼:

<td> <select name="xs_r_gender" id="xs_r_gender"> <option value="" selected>Select</option> <option value="male">Male</option> <option value="female">Female</option> </select></td>

感謝

回答

1

試試下面的代碼假設DOC = ie.document

doc.getElementById("xs_r_gender").value = "Male" 
+1

謝謝我嘗試過,但沒有運氣。 – 2013-02-25 11:57:38

+0

doc.getElementById(「xs_r_gender」)。value =「male」試試這個 – 2013-02-25 11:59:14

5

doc.getElementById("xs_r_gender").selectedindex=1
似乎這樣的伎倆。 (其中1代表男性)

雖然這意味着我需要做很多查找來確​​定我的下拉列表中的項目的價值。 (對於性愛來說足夠簡單,只有兩種選擇,但我有一些組合框可選多達50種)。如果有人知道更快的解決方案,那會很棒。在此期間,我開始做一些表!

謝謝。

0
Function SetSelect(s, val) As Boolean 

'Selects an item (val) from a combobox (s) 
'Usage: 
'If Not SetSelect(IE.Document.all.Item("tspan"), "Custom") Then 
'something went wrong 
'Else 
'continue... 
'End If 

Dim x As Integer 
Dim r As Boolean 
r = False 
For x = 0 To s.Options.Length - 1 
If s.Options(x).Text = val Then 
s.selectedIndex = x 
r = True 
Exit For 
End If 
Next x 

SetSelect = r 
End Function 
+0

有一點解釋也有幫助。 – RacerNerd 2014-05-16 22:41:01

+0

對不起。我以爲代碼中的註釋是自己解釋的......還是不是? = D – kazulius 2014-05-21 19:15:02

1

在您的代碼中使用它來調用下面的函數。

xOffset = SetSelect(IE.Document.all.Item("shipToStateValue"), "Texas") 
doc.getElementById("shipToStateValue").selectedindex = xOffset 

然後用這個你的函數

Function SetSelect(xComboName, xComboValue) As Integer 
    'Finds an option in a combobox and selects it. 

    Dim x As Integer 

    For x = 0 To xComboName.options.Length - 1 
     If xComboName.options(x).Text = xComboValue Then 
      xComboName.selectedindex = x 
      Exit For 
     End If 
    Next x 

    SetSelect = x 

End Function 
-1

複製從這裏到最後一行:

Sub Filldata() 

Set objShell = CreateObject("Shell.Application") 

IE_count = objShell.Windows.Count 

For X = 0 To (IE_count - 1) 

On Error Resume Next ' sometimes more web pages are counted than are open 

my_url = objShell.Windows(X).document.Location 

my_title = objShell.Windows(X).document.Title 

    If my_title Like "***Write your page name***" Then 

    Set IE = objShell.Windows(X) 

     Exit For 

    Else 

     End If 

    Next 

With IE.document.forms("***write your form name***") 

' Assuming you r picking values from MS Excel Sheet1 cell A2 

i=sheet1.range("A2").value 

.all("xs_r_gender").Item(i).Selected = True 

End with 

End sub 
+0

您能否詳細說明您的答案,並添加關於您提供的解決方案的更多描述? – abarisone 2015-04-10 13:19:13

0

試試這個代碼:

doc.getElementById("xs_r_gender").value = "Male" 
doc.getElementById("xs_r_gender").FireEvent("onchange") 
0

你可以這樣做這個:

doc.getElementsByName("xs_r_gender").Item(1).Selected=True

doc.getElementById("xs_r_gender").selectedindex = 1

其中1是男性選項(在這兩種情況下)。

如果Dropbox需要觸發某個事件以便知道您的選擇,那麼很可能它將成爲「onchange」事件。您可以啓動它,如下所示:

doc.getElementById("xs_r_gender").FireEvent("onchange")

如果你希望能夠選擇基於期權的文字,你可以使用Lansman(here)給出的功能選項。 根據相同的答案,如果您想通過其值屬性調用選項(而不是文本,則可以將行If xComboName.Options(x).Text = xComboValue Then更改爲If xComboName.Options(x).value = xComboValue Then)。

這應該涵蓋所有基地。