【问题标题】:Inserting value from vba into html list将值从 vba 插入 html 列表
【发布时间】:2017-03-21 04:19:41
【问题描述】:

我已经能够使用 vba 的 .value 方法来操作列表中的数据。但是当尝试更新特定值时,它会将值填充为空白,并且不会让我更改它。我正在尝试将日期推送到该字段中,但日期是从列表旁边的日历中获取的,该列表将日期放在日历的框中。使用 dom explorer 时,我只需更改选项值,它会更改字段中的日期。当我使用 value 方法来更改相同的选项时,它会将字段填充为空白。有什么想法吗?

HTML code of page

Picture of list with calendar

我已经能够使用这个来更改值:

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

任何已经存在但不能将其更改为日期的值,即使它接受日期也是如此。例如:

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

将列表留空,就像没有选择任何内容一样。除非我输入的日期已经是从日历中提取的列表的一部分,否则我无法更改日期。

【问题讨论】:

  • 使用 MS HTML 控件,并将 IHTMLSelectElement 类型的对象设置为您拥有的选择 27。 Intellisense 和当地人应该会帮助你,我认为 SelectedIndex 需要设置。它可能是一个 Jquery 或自定义日历控件,其中包含可以操作的其他内容...

标签: html excel vba internet-explorer html-content-extraction


【解决方案1】:

要选择选项,请使用selectedIndex 属性。要将新的option 元素添加到select 元素,首先使用doc.createElement("option") 创建它,然后在select 元素上调用add 方法。高温

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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-07
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多