打开Dialog IE Automation VBA

时间:2015-05-25 09:29:51

标签: vba excel-vba automation internet-explorer-10 openfiledialog

我试图登录网站,然后上传一些照片,但我没有得到任何进一步的信息。对话框打开后,我无法以编程方式控制它。我尝试将对象定义为FileDialog并使用Application.SendKeys,但似乎对话框不是应用程序。

2 个答案:

答案 0 :(得分:0)

您必须使用代码与此类似的浏览器对象:

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

依旧......

  • 使用DOM,尽可能避免使用SendKeys

  • "睡眠"使用可以在模块顶部声明的API,如下所示:

    私有声明Sub Sleep Lib" 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)

然后发送密钥;你可以发送"标签"键从一个文本框移动到下一个

如果网站使用此对话框允许您选择多个图像,那么自动化将更具挑战性;您必须使用其他录制方法来模拟多个文件选择或使用鼠标拖放操作

以下是一些有助于API功能的链接:

修改以解决3个问题:

  • "我使用该功能时出错。 64位系统的东西......" - 您有一个64位版本的Excel,所以替换此声明

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

  • "当我复制功能时,VBA表示只有评论可能会出现在End Sub ..."
  • 之后

这是一个声明,因此它必须放在模块的顶部(在任何其他功能之前)

  • 我应该写什么而不是" vbNullString"和" dialogCaption" ??

你仍然可以使用" vbNullString"作为第一个参数,但你必须替换" dialogCaption"使用对话框的标题

  • 不要对vbNullString
  • 使用引号
  • 对对话框的标题使用引号

答案 1 :(得分:0)

您好我的代码是这样的:

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 

谢谢Paul,我今天就试试吧!

最好的问候

Udar