从vba插入值到html列表中

我已经能够使用vba的.value方法来处理列表中的数据。 但是,当试图更新一个特定的价值时,它填补了空白的价值,不会让我改变它。 我试图将date推入该字段,但date取自列表中的日历旁边的日历,日历将date放在框中。 当使用DOM浏览器,我只是改变选项的价值,它会改变在该领域的date。 当我使用value方法来改变这个相同的选项时,它填充空白字段。 有任何想法吗?

页面的HTML代码

列表的图片与日历

我已经能够改变这个值:

IE.Document.getElementsByTagName("select")(27).Value = _ "Today@javascript:gs.daysAgoStart(0)@javascript:gs.daysAgoEnd(0)" 

对于已经存在的任何值,即使接受date也不能将其更改为date。 例如:

 IE.Document.getElementsByTagName("select")(27).Value = "2016-11-04" 

将列表留空,就好像没有select任何东西。 除非我input的date已经是从日历中提取的列表的一部分,否则我无法更改date。

要select选项,请使用selectedIndex属性。 要添加新的option元素select元素首先创build与doc.createElement("option") ,然后调用add方法select元素。 HTH

 Option Explicit ' Add reference to Microsoft Internet Controls (SHDocVw) ' Add reference to Microsoft HTML Object Library Sub NewOptionDemo() Dim ie As SHDocVw.InternetExplorer Dim doc As MSHTML.HTMLDocument Dim url As String url = "file:///c:/Temp/main.html" Set ie = New SHDocVw.InternetExplorer ie.Visible = True ie.navigate url While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE DoEvents Wend Dim htmlCombobox As MSHTML.HTMLSelectElement Set doc = ie.document Set htmlCombobox = doc.querySelector("select[id='select1']") If (htmlCombobox Is Nothing) Then MsgBox "No combobox with id 'select1' was found on the page." Else ' select first option htmlCombobox.selectedIndex = 0 ' create new option, add this option to combobox and select it Dim newOption As HTMLOptionElement Set newOption = doc.createElement("option") newOption.Value = "new-option" newOption.Text = "New option" htmlCombobox.Add newOption htmlCombobox.selectedIndex = 3 ' chnge value and text of first option and select it again htmlCombobox.Item(0).Value = "new-value-for-first-item" htmlCombobox.Item(0).Text = "new value for first item" htmlCombobox.selectedIndex = 0 End If ie.Quit Set ie = Nothing End Sub 

main.html中

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <!-- saved from url=(0014)about:internet --> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> </head> <body> <select id="select1"> <option value="option1">Option 1</option> <option value="option2">2016-11-04</option> <option value="option3" selected="selected">Option 3</option> </select> </body> </html>