简体   繁体   English

在excel VBA中使用发送键打开后,如何在继续之前检查文件是否已经打开?

[英]How to check if file is already open before proceeding after opening with send keys in excel VBA?

I am opening a file from a website.我正在从网站打开一个文件。 After I send sendkeys "%{O}", I need the file to be opened first before proceeding.发送sendkeys“%{O}”后,我需要先打开文件才能继续。 If I add wait times or loops after the sendkeys, the file wont open until after those loops.如果我在 sendkeys 之后添加等待时间或循环,则文件在这些循环之后才会打开。 Kindly note that I don't know the name or location of the file, I just opened it from a path given to me.请注意,我不知道文件的名称或位置,我只是从给定的路径打开它。

I have already tried putting Application.Wait, creating a sub that just counts and using this between opening and writing to the worksheet.我已经尝试放置 Application.Wait,创建一个仅计数的子程序,并在打开和写入工作表之间使用它。

Application.SendKeys "%{O}" 'opens the workbook from IE
Set wb2 = ActiveWorkbook.Worksheets("Needed_Worksheet")
wb2.Cells(1,1) = "HELLO"

I expect to write on the first cell of the newly opened workbook.我希望在新打开的工作簿的第一个单元格上书写。

This is working now using Sleep:这是现在使用睡眠工作:

#If VBA7 Then
    Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 'For 64 Bit Systems
#Else
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'For 32 Bit Systems
#End If

Then call:然后调用:

Sleep 20000

Thanks @DavidZemens for the suggestion感谢@DavidZemens 的建议

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

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