使用VBA向HTML页面输入值时出现Automation Error问题求助
Fixing Automation Error When Setting Textbox Value via VBA for Internet Explorer
Let's break down why your code is throwing that Automation Error and how to fix it:
First, the Critical Typo
Looking at your code, you used getElementsByID — but this method doesn't exist! The correct method to fetch a single element by its ID is getElementById (note the singular Element, not Elements). That's almost certainly the immediate cause of your error, since VBA can't find a method with that misspelled name.
Other Potential Issues & Fixes
Even if you fix the typo, relying on a fixed 10-second wait isn't reliable (page load times vary based on network speed, server response, etc.). Here's an improved version of your code that handles proper loading waits and element detection:
Sub test() Dim ie As Object Dim URL As String Dim targetElement As Object Set ie = CreateObject("internetexplorer.application") ie.Visible = True URL = "https://eos-bw.ubs.net/sap/bw/BEx?cmd=ldoc&template_id=ZSAT_CO_EP_1_2_KK_TAB_DRT_PERF&sap-language=EN" ie.navigate URL ' Wait for IE to finish loading completely Do While ie.Busy Or ie.ReadyState <> 4 DoEvents Loop ' Wait for the target element to exist (handles dynamic content loading) Set targetElement = Nothing Do While targetElement Is Nothing On Error Resume Next ' Corrected method name here Set targetElement = ie.document.getElementById("VAR_VALUE_EXT_2") On Error GoTo 0 DoEvents Loop ' Assign the value once the element is found targetElement.Value = "USD" ' Clean up objects to avoid memory leaks Set targetElement = Nothing Set ie = Nothing End Sub
Additional Checks to Make
- Verify the Element ID: Open the page in IE, press F12 to open Developer Tools, use the element picker to select your textbox, and confirm its ID is exactly
VAR_VALUE_EXT_2(sometimes SAP/BEx pages generate dynamic IDs, so this might change). - Check for iframes: If the textbox is inside an iframe, you'll need to access the iframe's content document first. For example:
' Adjust the index to match your iframe's position Dim iframe As Object Set iframe = ie.document.getElementsByTagName("iframe")(0) Set targetElement = iframe.contentDocument.getElementById("VAR_VALUE_EXT_2")
内容的提问来源于stack exchange,提问作者user3293280
相关产品推荐
相关产品推荐

