VBA调用Internet Explorer加载特定网站时无法获取ReadyState的问题
Hey there, let's work through this tricky IE automation problem you're facing with kwm.kromi.de! It's super frustrating when code works for most sites but fails for a specific one—let's break down the likely causes and fixes.
1. First, Fix the URL Protocol
Your current URL is missing the http:// or https:// prefix, which can cause IE to handle the address incorrectly (it might treat it as a local file or fail to resolve the domain properly). Update your URL line to:
URL = "https://kwm.kromi.de"
This ensures IE knows to load it as a web page, not a local resource.
2. Improve the Loading Wait Logic
The simple Do: Loop Until IE.readyState = READYSTATE_COMPLETE check often isn't enough for modern sites that use dynamic content (AJAX, lazy loading, etc.). Many sites will report READYSTATE_COMPLETE before all elements are actually loaded, and IE's Busy flag can also help confirm the page is done processing. Replace your loop with this more robust version:
' Wait for IE to finish loading Do While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE DoEvents ' Yields control to the OS so IE can process Loop ' Optional: Wait for the document to be fully ready (in case ReadyState lies) Do While IE.Document Is Nothing DoEvents Loop
This double-check ensures you don't try to interact with the page before it's actually ready.
3. Ensure Reliable IE Object Initialization
Using CreateObject("internetExplorer.application") can sometimes lead to inconsistent instances, especially if there are leftover IE processes running. Try explicitly referencing the Microsoft Internet Controls library first:
- Open the VBA Editor
- Go to Tools > References
- Check the box for Microsoft Internet Controls
- Click OK
Then initialize IE like this instead:
Dim IE As InternetExplorer Set IE = New InternetExplorer
This gives you better control over the IE instance and avoids potential "empty object" issues.
4. Handle Anti-Automation or Dynamic Content
Some sites detect and block IE automation, or have elements that load long after the initial page load. Here are a few fixes:
- Add a short forced wait after the initial load (for lazy-loaded elements):
Application.Wait Now + TimeValue("00:00:03") ' Wait 3 seconds - Enable silent mode to suppress pop-ups that might interrupt loading:
IE.Silent = True - Check if the site opens a new window/tab—sometimes sites redirect to a different frame or window, which would make your original IE object point to an empty page. You can loop through open IE windows to find the correct one:
Dim objIE As InternetExplorer For Each objIE In New InternetExplorer If InStr(objIE.LocationURL, "kwm.kromi.de") > 0 Then Set IE = objIE Exit For End If Next objIE
5. Consider IE Compatibility
Keep in mind that Microsoft has deprecated IE, and many modern sites no longer support it properly. It's possible kwm.kromi.de uses features that IE can't handle, leading to incomplete loads or unexpected behavior. If possible, you might want to explore using Edge WebDriver with VBA (via Selenium) for better compatibility with modern sites.
Once you've got the loading issue sorted, you can start adding code to handle login and generate the Excel file—just make sure all element interactions (like filling username/password, clicking buttons) happen after the page is fully ready.
内容的提问来源于stack exchange,提问作者MaciekPol




