Code:
Private Declare Sub AppSleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
Public Sub PauseApp(PauseInSeconds As Long)
Call AppSleep(PauseInSeconds) 'Used to be actual seconds, but this program doesn't recognize them. OY!
End Sub
Option Explicit
Sub AccessForumsDemo()
Dim OIE As InternetExplorer
Set OIE = New InternetExplorer
Dim Loaded As Boolean
OIE.navigate ("https://www.accessforums.net/index.php")
OIE.Visible = True
Call SleepIE(OIE)
''''This Block is using hard-coded HTML structure. And shows why I'd like to be able to pass it in into the function.
'Failing Test
Loaded = ConfirmLoaded(OIE, "Access Forums", 500, 10)
If Loaded = False Then
MsgBox "This failed because 'confirmloaded' is using oie.document.forms(0).innertext"
End If
'Successful Test
Loaded = ConfirmLoaded1(OIE, "Microsoft Access Forums", 500, 10)
If Loaded = True Then MsgBox "Access Forums text was found, oh happy day."
''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''What I would like to be able to do. ' When "HTMLStruct" is set as a string, the code will execute, but the innertext is never found.
Loaded = AttemptingFunction(OIE, "Microsoft Access Forums", 500, 10, , "OIE.Document.body.innerText")
If Loaded = True Then MsgBox "Items found"
If Loaded = False Then MsgBox "Still not working"
End Sub
Function ConfirmLoaded(OIE As InternetExplorer, TextNeeded As String, MSecs As Integer, MaxLoop As Integer, Optional AttemptNo As Integer)
'This is used as a confirmation that IE is actually on the correct page. Its more reliable than readstate waits in IE. Things might need to be
'adjusted depending on the page. change innertext to value, or extend out the oie statement to look inside an iframe.
'Use Example: Loaded = ConfirmIE(oie, "Important Short Sale Update", 500, 40)
'Use Example: If Loaded = False Then GoTo CClick ' loaded is false, it means the text was not found in the maxtime
On Error GoTo Errs:
MaxTime = MSecs * MaxLoop / 1000 * AttemptNo
Do Until InStr(OIE.Document.forms(0).innerText, TextNeeded) > 0
Call SleepIE(OIE)
Call AppSleep(MSecs)
If LoopCt = MaxLoop Then GoTo ExitLoop
LoopCt = LoopCt + 1
Loop
ConfirmLoaded = True
GoTo Exit_Sub:
ExitLoop:
MsgBox "Timeout Message Box" & vbCr & "TextNeeded = " & TextNeeded & vbCr & "Max wait time in Seconds = " & MaxTime & vbCr & "Oie Page = " & OIE.LocationURL
Errs:
If Err.Number = -2147467259 Then
Debug.Print "Bad HTML structure"
End If
Exit_Sub:
End Function
Function ConfirmLoaded1(OIE As InternetExplorer, TextNeeded As String, MSecs As Integer, MaxLoop As Integer, Optional AttemptNo As Integer)
'This is used as a confirmation that IE is actually on the correct page. Its more reliable than readstate waits in IE. Things might need to be
'adjusted depending on the page. change innertext to value, or extend out the oie statement to look inside an iframe.
'Use Example: Loaded = ConfirmIE(oie, "Important Short Sale Update", 500, 40)
'Use Example: If Loaded = False Then GoTo CClick ' loaded is false, it means the text was not found in the maxtime
MaxTime = MSecs * MaxLoop / 1000 * AttemptNo
Do Until InStr(OIE.Document.body.innerText, TextNeeded) > 0
Call SleepIE(OIE)
Call AppSleep(MSecs)
If LoopCt = MaxLoop Then GoTo ExitLoop
LoopCt = LoopCt + 1
Loop
ConfirmLoaded1 = True
GoTo Exit_Sub:
ExitLoop:
MsgBox "Timeout Message Box" & vbCr & "TextNeeded = " & TextNeeded & vbCr & "Max wait time in Seconds = " & MaxTime & vbCr & "Oie Page = " & OIE.LocationURL
Exit_Sub:
End Function
Function AttemptingFunction(OIE As InternetExplorer, TextNeeded As String, MSecs As Integer, MaxLoop As Integer, Optional AttemptNo As Integer, Optional HTMLStruct As String)
'This is used as a confirmation that IE is actually on the correct page. Its more reliable than readstate waits in IE. Things might need to be
'adjusted depending on the page. change innertext to value, or extend out the oie statement to look inside an iframe.
'Use Example: Loaded = ConfirmIE(oie, "Important Short Sale Update", 500, 40)
'Use Example: If Loaded = False Then GoTo CClick ' loaded is false, it means the text was not found in the maxtime
If HTMLStruct = "" Then HTMLStruct = OIE.Document.body.innerText
MaxTime = MSecs * MaxLoop / 1000 * AttemptNo
Do Until InStr(HTMLStruct, TextNeeded) > 0
Call SleepIE(OIE) ' readywait, ussually works. Not always though.
Call AppSleep(MSecs) 'Hard pause for so many miliseconds. Adjust MSecs depending on the website
If LoopCt = MaxLoop Then GoTo ExitLoop
LoopCt = LoopCt + 1
Loop
AttemptingFunction = True ' IF the innertext, innerhtml or whatever we want is found. Its true.
GoTo Exit_Sub:
ExitLoop:
MsgBox "Timeout Message Box" & vbCr & "TextNeeded = " & TextNeeded & vbCr & "Max wait time in Seconds = " & MaxTime & vbCr & "Oie Page = " & OIE.LocationURL
Exit_Sub:
End Function