PublicSub WaitForPageReady(ByRef aWebObj As Object, ByVal aWebStr As String, Optional ByVal aTimeOut As Long = 10) Dim vFrame As HTMLDocument Dim vElement As Object Dim vBuffer As Variant Dim vTimeLimit As Date vTimeLimit = DateAdd("s", aTimeOut, VBA.Now)
OnErrorResumeNext
SelectCase aWebStr Case"Page1"' depends on different kind of page, the "last load object" is different DoWhile vBuffer <> "theTarget" Set vFrame = aWeb.Object.Document Set vElement = vFrame.Document.GetElementsByClassName("some class name here") vBuffer = vElement.innerText ' get the compare string IfDateDiff("s", vTimeLimit, VBA.Now) > 0Then Err.Raise ' Add Err message here EndIf DoEvents Loop EndSelect
EXIT_CleanUp: OnErrorGoTo0 Err.Clear ExitSub ERR_CleanUp: OnErrorGoTo0 Err.Raise ' Add Err message here ERR_Handler: ' Store Err message Resume ERR_CleanUp: EndSub
在pass这两个function后就可以开始scripting网页上的内容了. 注意,在access 网页object的时候,除了class name,还有frame的区别. 需要在“可以读取的frame”内,access 相应的object,不然读不到. 辨别方式:在inspect,select element in the page to inspect看看是否有不同的frame.
在这里查看属于哪一个frame.
注意vFrame.Document.GetElementsByClassName -> 根据不同需求,可以用getElementById等其他function. Mentor的代码是自己搓了个类似WaitForBrowserReady的Function. 如果一定时间内getElement function没有get到target的内容,也time out 给error然后退出.
PublicSub CreateSession(OptionByVal aProfileName As String = "") Dim xErrNum As Long, xErrMsg As String
Dim vSessionCount As Long Dim vDone As Boolean Dim vI As Long Dim vTimeOut As Date Dim vSessionString As Strnig, vSessionChar As String Dim vResult As Long
' ensure a profile was requested If aProfileName = ""Then Err.Raise EX_VALUE_NULL, ERR_SOURCE, "Unable to open session" EndIf
' set the config file path and name If aProfileName = "applicationA"Then aProfileName = Environ("AppData") & "\thePath\ofTheApplication.exe" Else aProfileName = Environ("AppData") & "\thePath\" & aProfileName EndIf
' free handle if it already exists If mHasHandle = TrueThen ReleaseHandle EndIf
' use the connection manager to test for any open mainframe windows Set theConnMgr = CreateObject("theShell") theConnMgr.theClass.Refresh vSessionCount = theConnMgr.theClass.Count
' create new session interface object Set thePS = theConnMgr.autECLPS ' autPS Set theWin = theConnMgr.window ' autECLSession Set theOIA = theConnMgr.autECLOIA ' AutOIA
' find used session tags vSessionString = "" If vSessionCount > 0Then For vI = 1To vSessionCount vSessionString = vSessionString & theConnMgr.autECLConnList(vI).Name Next vSessionString = UCase(vSessionString) EndIf
' find available session tags If vSessionString = ""Then vSessionChar = "Z" Else For vI = 90To65Step-1' check for existing sessions from Z to A and use next available IfInStr(1, vSessionString, Chr(vI)) < 1Then vSessionChar = Chr(vI) ExitFor EndIf Next EndIf
' use the connection manager to open a new session mSessionID = Asc(vSessionChar) - 64 vResult = theConnMgr.OpenSession(SESSION_TYPE, mSessionID, aProfileName, SESSION_TIMEOUT, SESSION_PAINT_CNT) If vResult Then MsgBox "Error connecting to the system", 0 ExitSub EndIf
' wait for session to launch vTimeOut = DateAdd("s", LAUNCH_TIMEOUT, VBA.Now) vDone = False Do Until vDone = True ' session manager never updates the 'count' after creation, so create it each time through the loop theConnMgr.autECLConnList.Refresh
' attemp to find the created session If theConnMgr.autECLConnList.Count > 0Then For vI = 1To theConnMgr.autECLConnList.Count If theConnMgr.autECLConnList(vI).Name = vSessionChar Then mHandle = theConnMgr.autECLConnList(vI).Handle thePS.SetConnectionByHandle mHandle theOIA.SetConnectionByHandle mHandle mHasHandle = True vDone = True ExitFor EndIf Next EndIf
' timeout if the session wasn't found in the given time frame If vDone = FalseAnd vTimeOut < VBA.Now Then Err.Raise EX_TIMEOUT, ERR_SOURCE, "Unable to get session handle within timeout limit" EndIf
DoEvents Loop
' give the mainframe up until the timeout limit to respond If theOIA.WaitForInputReady(INTERFACE_TIMEOUT) = FalseThen Err.Raise EX_TIMEOUT, ERR_SOURCE, "Session failed to respond within" & INTERFACE_TIMEOUT / 1000 & " second timeout limit" EndIf