我正在自动执行网络剪贴,并且出现对象错误438对象不支持此属性或方法。在vba中,当我到达最后一行代码时。如果我在Internet Explorer控制台上运行它,则会得到该值,但会在vba中出错。有帮助吗?
Dim shellWins As ShellWindows
Dim IE As Object
Set shellWins = New ShellWindows
If shellWins.Count > 0 Then
' Get IE
Set IE = shellWins.Item(0)
Else
' Create IE
Exit Sub
End If
IE.Navigate "https://mywebpage.com"
While IE.Busy
DoEvents
Wend
Do Until IE.ReadyState = 4
DoEvents
Loop
Dim rtn As String
rtn = IE.getElementById("myID").getAttribute("value") << I get ERROR here
答案 0 :(得分:1)
该方法是.document(HTMLDocument)对象的属性,而不是IE对象
IE.document.getElementById("myID")
要创建IE实例,请执行以下操作:
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
答案 1 :(得分:1)
是否可以将对象设置为IE的现有实例?
因此,您可以采取以下措施来抓住一个已经存在的IE浏览器:
Dim IE As Object
Set IE = GetIE("https://mywebpage.com")
Function GetIE(sLocation As String) As Object
Dim objShell As Object, objShellWindows As Object, o As Object
Dim sURL As String
Dim RetVal As Object
Set objShell = CreateObject("shell.application")
Set objShellWindows = objShell.Windows
For Each o In objShellWindows
sURL = ""
On Error Resume Next
sURL = o.document.Location
On Error GoTo 0
If sURL Like "*" & sLocation & "*" Then
Set RetVal = o
Exit For
End If
Next o
Set GetIE = RetVal
End Function
现在要实际问题。在没有看到HTML代码或网站本身的情况下,无法精确地为您提供帮助。
但是,您可以尝试使用以下任一属性来查看它们是否起作用(按此处列出的顺序尝试):
Dim rtn As String
rtn = IE.Document.getElementById("myID").Value
Dim rtn As String
rtn = IE.Document.getElementById("myID").innerText
Dim rtn As String
rtn = IE.Document.getElementById("myID").outerText
答案 2 :(得分:0)
要同意上述一些答案。下车IE,除非有限制。
您根本没有创建IE对象。因此,您正在尝试使用不存在的对象。
string word = "Hello";
string result = string.Empty;
foreach(char c in word) //loop through each character of word
{
result += (char)(c + 2); //Add 2 to character and append it to result after converting back to character
}
Console.WriteLine(result);
答案 3 :(得分:-2)
首先:不要使用IE。 Firefox和Chrome更加可行,并且实际上符合HTML。
万一这不能解决问题:此外,如果您的脚本在启动时运行并且在HTML之前声明,它将尝试搜索当前缓冲区,并且由于尚未加载该对象而找不到该对象。尝试将脚本放在HTML声明之后。
很抱歉,如果我误解了上面的代码,因为我实际上不会说VB ^^'