作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的应用程序当前导航到供应商网站,登录,打开报告,单击 IFrame 后面的下载按钮(这是癌症开始工作),然后点击打开/保存对话框上的打开按钮。现在项目的这个阶段剩下的就是让 excel 等待新工作簿打开,将信息拉到新工作表中,然后关闭另一个工作簿。
问题是,无论我尝试什么,新工作簿都不会在宏运行时打开。如果我在代码中添加一个中断,一切都很好。我尝试过 application.wait、sleep、DoEvents 甚至是 msgbox。我在这里有点难过。
'This clicks the button that initiates the open/save dialog box in IE
iWin.contentDocument.querySelector("a[title=Excel][onclick*=EXCELOPENXML]").Click
'set the name of the windows the program should look for
iStr = "Reports LeaveXpert Application - Internet Explorer"
eStr = "Case Detail.xlsx [Protected View] - Excel"
'these are declared as longptr
leHwin = 0
cdHwin = 0
'Grabs the windows handle for the IE window
leHwin = FindWindow(vbNullString, iStr)
'Makes sure the IE window is active
i = SetForegroundWindow(leHwin)
'a little bit of delay
Application.Wait DateAdd("s", 5, Now)
'Send the key combination for the open button to the open browser
Application.SendKeys "%{O}"
cdHwin = 0
Debug.Print cdHwin
'this should wait for the new spreadsheet to open but instead the application hangs here and the new spreadsheet never opens.
Do While cdHwin = 0
'various methods of having the program wait that i've tried
'DoEvents
'Application.Wait (Now() + TimeValue("00:00:01"))
'Sleep 1000
'this should set the variable to the windows handle of the new sheet once it opens causing the loop to stop
cdHwin = FindWindow(vbNullString, eStr)
'This just keeps giving me 0 as an output
Debug.Print cdHwin
Loop
'we never get to this line of code unless I add a break somewhere in the loop, then the sheet opens and I get the windows handle as output.
Debug.Print cdHwin
更新:
'I made 2 changes that seem to have resolved the issue.
Private Sub AppEvents_WorkbookOpen(ByVal Wb As Workbook)
If Left(Wb.Name, 11) = CaseDetailFileName Then
'This sub that handles the transfer of data from the newly opened workbook to this workbook.
'It had an internal call to close the newly opened workbook that I forgot about, so I removed it
ImportCase Wb
'a duplicate call also existed out here, there were also a few duplicate ".activate"
'commands inside the import sub and this if statement that I removed
Wb.Close
'This sub handles data sorting and analytics
CaseFAS
End If
'I had tried copying this line from Workbook_Open a few times before and it
'didn't resolve the issue by itself,
'but I'm reasonably sure it is part of the solution
Set AppEvents = Me.Application
End Sub
为了完整起见,在 ImportCase 的引擎盖下一瞥。
'This sub handles importing the optis workbook when it opens. It's called by
'the AppEvents_WorkbookOpen sub declared in ThisWorkbook
Public Sub ImportCase(ByVal book As Workbook)
Dim srcSht As Worksheet
Set srcSht = ThisWorkbook.Sheets("OptisSource")
'This line clears out any old data
srcSht.Cells.Clear
'Sets the newly opened workbook to be the active sheet
book.Sheets("Case Detail").Activate
'Copys the information and sends it to a predefined sheet in this workbook
ActiveSheet.UsedRange.Copy Destination:=srcSht.Range("A1")
'sets the newly imported information to the active sheet
srcSht.Activate
'This sub clears out ghost data
ClearNulls
End Sub
最佳答案
如果我们只是让 Excel 做它的事情,并遵循事件驱动/去同步范式的流程呢?Excel.Application
类引发了许多您可以在任何类模块中处理的事件,但 ThisWorkbook
模块可以做 - 你声明一个 WithEvents
模块级变量:
Option Explicit
Private WithEvents AppEvents As Excel.Application
然后你用
Application
初始化它对象引用,可能在打开状态 - 如果我们在
ThisWorkbook
模块,我们继承了
Application
来自基地的属性(property)
Excel.Workbook
类(class):
Option Explicit
Private WithEvents AppEvents As Excel.Application
Private Sub Workbook_Open()
Set AppEvents = Me.Application
End Sub
声明
WithEvents
变量使它在左上角的下拉列表中可用,然后我们可以选择一个成员来使用右上角的下拉列表来实现:
Application
处理的事件之一级别,是比较方便的
WorkbookOpen
给我们一个
Wb As Workbook
的事件对刚刚打开的工作簿的引用:
Private Sub AppEvents_WorkbookOpen(ByVal Wb As Workbook)
End Sub
在这里,您可以拦截在启用宏的 session 期间打开的每个工作簿,只要这个
AppEvents
引用仍然在范围内——也就是说,直到
ThisWorkbook
已经关了。
ThisWorkbook
可能看起来像这样:
Option Explicit
Private Const CaseDetailFileName As String = "Case Detail.xlsx"
Private WithEvents AppEvents As Excel.Application
Private Sub Workbook_Open()
Set AppEvents = Me.Application
End Sub
Private Sub AppEvents_WorkbookOpen(ByVal Wb As Workbook)
If Wb.Name = CaseDetailFileName Then
DoThingWithCaseDetailWorkbook Wb
End If
End Sub
然后
Public Sub DoThingWithCaseDetailWorkbook(ByVal book As Workbook)
可以在任何标准模块中定义,并且可以访问在
WorkbookOpen
之前设置的任何模块状态/变量。事件触发。
关于excel - 如何让excel等待工作簿打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67493997/
我是一名优秀的程序员,十分优秀!