简体   繁体   English

打开对话框IE Automation VBA

[英]Open Dialog IE Automation VBA

I'm trying to log into a website and then upload some photos but I'm not getting any further. 我正在尝试登录一个网站,然后上传一些照片,但是没有任何其他信息。 When the dialog opens, I can't control it programmatically. 对话框打开时,我无法以编程方式对其进行控制。 I tried to define an object as FileDialog and also to use the Application.SendKeys but it seems that the dialog isn't an application. 我试图将一个对象定义为FileDialog并也使用Application.SendKeys但似乎该对话框不是应用程序。

You'll have to use a browser object with code similar to this: 您必须使用具有以下代码的浏览器对象:

.

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

.

and so on... 等等...

  • Use DOM, and try to avoid SendKeys if possible 使用DOM,并尽可能避免使用SendKeys

  • "Sleep" uses an API that can be declared at the top of the module like this: “睡眠”使用可以在模块顶部声明的API,如下所示:

    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 私有声明子睡眠库“ kernel32”(ByVal dw毫秒)

.

Edit : I noticed that you are actually trying to interact with a dialog box, not a browser page. 编辑 :我注意到您实际上是在尝试与对话框而不是浏览器页面进行交互。 If so, you'll need to use Windows API functions to be able to first identify it from VBA; 如果是这样,您将需要使用Windows API函数才能首先从VBA进行识别; I'd suggest using "FindWindow": 我建议使用“ FindWindow”:

Private Declare Function FindWindow Lib "User32" Alias "FindWindowA" ( _
 ByVal lpClassName As String, _
 ByVal lpWindowName As String _
) As Long

and call it from VBA like this: 并从VBA这样调用它:

setMyDialog = FindWindow(vbNullString, dialogCaption)

then send keys to it; 然后发送密钥给它; you can send the "Tab" key to move from one text box to the next 您可以发送“ Tab”键从一个文本框移至下一个文本框

If the website is using this dialog to allow you to select multiple images it will be more challenging to automate; 如果网站使用此对话框允许您选择多个图像,则自动化将更具挑战性。 you'll have to use other recordings methods to simulate multiple file selections or drag-and-drop actions with the mouse 您将不得不使用其他录制方法来模拟多个文件选择或使用鼠标进行拖放操作

Here are some links that can help with API functions: 以下是一些有助于API功能的链接:

.

Edit for your 3 questions: 编辑您的3个问题:

  • "I get an error when i use the function. Something with the 64-bit system..." - you have a 64 bit version Excel so replace this declaration “使用该函数时出现错误。64位系统出现问题...”-您有64位版本的Excel,因此请替换此声明

.

Private Declare Function FindWindow Lib "User32" Alias "FindWindowA" ( _
     ByVal lpClassName As String, _
     ByVal lpWindowName As String _
) As Long

with this one: 与此:

Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" ( _
     ByVal lpClassName As String, _
     ByVal lpWindowName As String
) As LongPtr

.

  • "When i copy the function VBA says that only comments may appear after End Sub..." “当我复制函数VBA时,在End Sub之后只能显示注释...”

This is a declaration so it must be placed at the top of the module (before any other functions) 这是一个声明,因此必须将其放在模块的顶部(在任何其他函数之前)

  • What should I write instead of "vbNullString" and "dialogCaption"?? 我应该写什么而不是“ vbNullString”和“ dialogCaption”?

You can still use "vbNullString" as the first argument, but you have to replace "dialogCaption" with the title of your dialog box 您仍然可以使用“ vbNullString”作为第一个参数,但是必须用对话框的标题替换“ dialogCaption”

  • do not use quotation marks for vbNullString 不要对vbNullString 使用引号
  • do use quotation marks for the title of the dialog 不要在对话框标题中使用引号

Hello my code is like this : 你好我的代码是这样的:

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 

Thank you Paul i'll try it today out! 谢谢保罗,我今天将尝试一下!

Best Regards 最好的祝福

Udar 乌达

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM