我试图登录一个网站,然后上传一些照片,但我没有任何进一步的进展。当对话框打开时,我无法以编程方式控制它。我试图将一个对象定义为FileDialog并使用Application.SendKeys,但该对话框似乎不是一个应用程序。
发布于 2015-05-25 22:53:00
您必须使用带有类似以下代码的浏览器对象:
。
Set browser = CreateObject("InternetExplorer.Application")
browser.Visible = True
brwser.navigate "http://stackoverflow.com/..." 'your address
Sleep 1000 'wait for 1 second
With browser.Document
Set txtUsr = .getElementsByName("Username")
Set txtPass = .getElementsByName("UserPass")
Set btnLogin = .getElementById("btnLogin")
End With
txtUsr.innerText = "User Name"
txtPass.innerText = "Password"
btnLogin.Click。
以此类推。
私有声明子休眠库"kernel32“(ByVal dwMilliseconds As Long)
。
编辑:我注意到您实际上是在尝试与对话框交互,而不是与浏览器页面交互。如果是这样的话,您需要使用Windows API函数才能首先从VBA中识别它;我建议使用"FindWindow":
Private Declare Function FindWindow Lib "User32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String _
) As Long并从VBA调用它,如下所示:
setMyDialog = FindWindow(vbNullString, dialogCaption)然后将键发送到它;您可以发送"Tab“键从一个文本框移动到下一个文本框
如果网站使用此对话框来允许您选择多个图像,则自动化将更具挑战性;您必须使用其他录制方法来模拟多个文件选择或使用鼠标的拖放操作
以下是一些对API函数有帮助的链接:
。
为您的3个问题编辑:
。
Private Declare Function FindWindow Lib "User32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String _
) As Long使用这一条:
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String
) As LongPtr。
这是一个声明,因此它必须放在模块的顶部(在任何其他函数之前)
我应该写什么而不是"vbNullString“和"dialogCaption"???
您仍然可以使用"vbNullString“作为第一个参数,但必须将"dialogCaption”替换为对话框的标题
发布于 2015-05-26 20:10:39
你好我的代码是这样的:
1.I define an obj as internetexplorer.aplication
2.I login and then I want to upload a photo so I click the button for upload
' all works till now
3.Now I try to interact with the dialog that let's me to choose the photos programmatically and their is my problem 谢谢你,保罗,我今天就试一试!
诚挚的问候
Udar
https://stackoverflow.com/questions/30435137
复制相似问题