在 VBA 中的 Internet Explorer 中的文本框中设置文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20782680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 01:05:30  来源:igfitidea点击:

Set text in textbox in internet explorer in VBA

excelinternet-explorervbaexcel-vba

提问by user603031

This is the HTML code of the textbox:

这是文本框的 HTML 代码:

<td>
  <input type="text" 
         name="maskString1" 
         size="32" 
         style="text-transform: uppercase" 
         onkeyup="determineType(this, document.accForm.searchType1)" 
         onblur= "mask('masksearchString1','searchString1','1', document.accLookForm.searchType1 )" 
         onfocus="unmask('maskString1','searchString1',document.accForm.searchType1,'1')"
  >

I have automated it in VBA Code:

我已经在 VBA 代码中自动化了它:

ie.Document.All("masksearchString1").Value = "0000000044455458"

But the problem is this text box is a cascading textbox. After we enter value in the textbox the value of the next combobox changes accordingly.

但问题是这个文本框是一个级联文本框。在文本框中输入值后,下一个组合框的值会相应更改。

But while running this code the value gets pasted in the textbox but the combobox value does not get changed.

但是在运行此代码时,该值会粘贴到文本框中,但组合框值不会更改。

Is there any way to place the cursor in the textbox and then paste the value so that the cascading functionality works?

有什么方法可以将光标放在文本框中,然后粘贴该值,以便级联功能起作用?

回答by dee

Try 'Focus()'or 'FireEvent()'methods. Short example here. HTH

尝试“Focus()”“FireEvent()”方法。简短的例子在这里。HTH

' Add refernces to
' - Microsoft Internet Controls
' - Microsoft HTML Object Library

Option Explicit

Private Const URL As String = "C:/Temp/page1.html"

Sub test()
    Dim internet_browser As InternetExplorer
    Dim html_document As HTMLDocument

    Set internet_browser = New InternetExplorer

    With internet_browser
        .Visible = True
        .navigate URL

        Do While .Busy And Not .readyState = READYSTATE_COMPLETE
            DoEvents
        Loop

        Set html_document = .document

        Dim x As HTMLInputElement
        Set x = html_document.all("maskString1222")
        If Not x Is Nothing Then
            ' x.Focus ... gets focus
            x.Value = "0000000044455458"
            x.FireEvent "onkeyup" ' ... or fire some other event by its name
        End If

Quit:
        .Quit
    End With

    Set html_document = Nothing
    Set internet_browser = Nothing
End Sub