No Valid COM Object!
Return, ComObj(9,ComObjQuery(pdoc,IID,IID),1),ObjRelease(pdoc)
This error happens intermittently and is driving me nuts! I've tried for days and it's just over my head to figure out. Any help?
The error occurs on line 61, which calls the function on line 126 which calls the WBGet() function on line 452. The WBGet() function is sending the error. (look for the ; ************************************************************************************************************************************ marker lines)
Code: Select all
F7::
{
tablets := new tablets
tablets.runTablets()
return
}
class tablets
{
runTablets()
{
Clipboard :=
Click
Send, ^a
Send, ^c
Clipwait, 2
Click
sleep, 10
itemDescription := Clipboard
this.markings := this.getTabletMarkings(itemDescription)
if (not this.markings)
{
msgbox, Tablet markings not found
return
}
this.color := this.getTabletColor(itemDescription)
this.shape := this.getTabletShape(itemDescription)
this.tabletNumber := this.longNumberToShortNumber(itemDescription)
this.nNumber := this.getNNumber(itemDescription)
pwb := ComObjCreate("InternetExplorer.Application") ;create IE Object
pwb.visible:=true ; Set the IE object to visible
WinMaximize, % "ahk_id " pwb.HWND
pwb.Navigate("https://www.drugs.com/imprints.php?imprint=" . this.markings . "&color=" . this.color . "&shape=" . this.shape) ;Navigate to URL
while pwb.busy or pwb.ReadyState != 4 ;Wait for page to load
{
Sleep, 100
}
URL := pwb.LocationURL ;grab current url
while (InStr(URL, "php?") and WinExist("ahk_class IEFrame")) ; waits for user to choose a tablet from a list, or until user closes explorer
{
URL := pwb.LocationURL ;grab current url
sleep, 100
}
If (not WinExist("ahk_class IEFrame"))
{
return
}
while pwb.busy or pwb.ReadyState != 4 ;Wait for page to load
{
Sleep, 100
}
tabletData:=pwb.document.GetElementsByTagName("DL")[0].innertext ;Get Tagname and Array value
; sleep, 100
; clipboard := tabletData
; msgbox, %tabletData%
this.source := "Drugs.com"
this.logo := "Yes"
; ************************************************************************************** the next line fails intermittently *****************************************************************
this.drug := this.getTabletDrug(tabletData) ; This function (actually WBGet()) fails intermittently.
this.dose := this.getTabletDose(tabletData)
this.isControlled := this.getTabletControlled(tabletData)
this.printTablet(pwb)
this.putTabletDateIntoMatrix()
this.putTabletLogoInfoInMatrix()
this.putTabletNRIntoMatrix(itemDescription)
SetTitleMatchMode, 2
WinClose, Internet Explorer
SetTitleMatchMode, 1
pwb := ""
this.moveToWeight(itemDescription)
return
}
getTabletMarkings(text)
{
RegExMatch(text, "U)marked ""(.*)""", tabletMarkings)
markings := StrReplace(tabletMarkings1, "<>", " ")
return markings
}
getTabletColor(text)
{
colorArray := {white:12, yellow:13, red:10, purple:-1, pink:8, peach:-1, orange:-1, maroon:-1, green:6, grey:5, gold:4, clear:3, brown:2, blue:1, black:73, beige:-1}
for k,v in colorArray
{
If (RegexMatch(text, k) and not InStr(text, "/" . k) and not InStr(text, k . "/"))
{
color := v
}
if (color = -1)
{
color := ""
return color
}
else if (color > 0)
{
return color
}
}
color := ""
return color
}
getTabletShape(text)
{
shapeArray := {round:24, capsule:5, oblong:5, oval:11, elliptical:11, rectangular:23, rectangle:23, triangular:32, triangle:32}
for k,v in shapeArray
{
if (RegexMatch(text, k))
{
shape := v
}
if (shape > 0)
{
return shape
}
}
shape := "0"
return shape
}
; *************************************************************************************************************************************
getTabletDrug(text)
{
RegExMatch(text, "U)Generic Name:(.*)Imprint", Drug)
if (not drug)
{
pwb := this.WBGet()
drug1 := pwb.document.GetElementsByTagName("H3")[0].innertext ;Get Tagname and Array value
}
drug1 := StrReplace(drug1, " bitartrate","")
drug1 := StrReplace(drug1, " hydrochloride","")
drug1 := StrReplace(drug1, " citrate","")
drug1 := StrReplace(drug1, " phosphate","")
drug1 := StrReplace(drug1, " Dipotassium","")
drug1 := StrReplace(drug1, " (Sublingual)","")
drug1 := StrReplace(drug1, " extended release","")
drug1 := StrReplace(drug1, " and ","/")
return Drug1
}
getTabletDose(text)
{
RegExMatch(text, "U)Strength:(.*)Color", Dose)
dose1 := StrReplace(dose1, "/ ","/")
dose1 := StrReplace(dose1, " /","/")
dose1 := StrReplace(dose1, " (base)","")
return Dose1
}
getTabletControlled(text)
{
RegExMatch(text, "U)CSA Schedule:(\d)", Schedule)
if (Schedule)
{
controlled := true
}
else
{
controled := false
}
return controlled
}
getNNumber(text)
{
If (RegExMatch(text, "U)marked\s+"".*"" .*(obscured|partial)"))
{
nNumber := this.tabletNumber + 1
}
else
{
nNumber := this.tabletNumber
}
return nNumber
}
printTablet(pwb)
{
WinGetText, text, Evidence Receiving - PRODUCTION
RegExMatch(text, "m)^\d{2}-\d{5,6}", matrixCaseNumber)
WinGetText, text, Evidence Examination Analytical Report
RegExMatch(text, "m)^\((Results|Empty)\)\s+\K[\d\.]+", item)
item := StrReplace(item, ".", "-")
this.ClickLink(pwb,Text:="Print")
WinWait, Print
WinActivate, Print
WinWaitActive, Print
Control, ChooseString, Adobe PDF, SysListView321, Print
SetControlDelay, -1
ControlClick, Button13, Print,,,, NA
SetControlDelay, 50
WinWait, Save PDF File As
WinActivate, Save PDF File As
WinWaitActive, Save PDF File As
IfNotExist, % library.activeCasesDir . matrixCaseNumber . "\"
{
FileCreateDir, % library.activeCasesDir . matrixCaseNumber . "\"
}
controlText := library.activeCasesDir . matrixCaseNumber . "\Drugs-" . item . ".pdf"
ControlSetText, Edit1, , Save PDF File As
ControlFocus, Edit1, Save PDF File As
Send, % controlText . "`r`n"
WinWait, Drugs
WinClose, Drugs
return
}
putTabletDateIntoMatrix()
{
ImageSearch, x, y, 0, -240, 2499, 1439, %A_ScriptDir%/Img/Matrix/DateClosed.bmp
if (ErrorLevel != 0)
{
ImageSearch, x, y, 0, -240, 2499, 1439, %A_ScriptDir%/Img/Matrix/DateOpen.bmp
if (ErrorLevel !=0)
{
msgbox, Date Input Error
return
}
else
{
library.pressMatrixButton("DateOpen")
library.randomWait()
mousemove, 0, 0
sleep, 100
}
}
library.pressMatrixButton("DateClosed")
library.randomWait()
Send {TAB}
if (A_MM<10)
{
send, %A_MM%{LEFT}{BACKSPACE}{SPACE}{RIGHT}/%A_DD%/%A_YYYY% {TAB} %A_MM%{LEFT}{BACKSPACE}{SPACE}{RIGHT}/%A_DD%/%A_YYYY%
}
if (A_MM>9)
{
send, %A_MM%/%A_DD%/%A_YYYY% {TAB} %A_MM%/%A_DD%/%A_YYYY%
}
mousemove, 0, 0
sleep, 100
library.randomWait(2)
library.pressMatrixButton("DateOpen")
library.randomWait()
return
}
putTabletLogoInfoInMatrix()
{
dose := this.dose
drug := this.drug
ImageSearch, x, y, 0, -240, 2499, 1439, %A_ScriptDir%/Img/Matrix/LogoClosed.bmp
if (ErrorLevel != 0)
{
ImageSearch, x, y, 0, -240, 2499, 1439, %A_ScriptDir%/Img/Matrix/LogoOpen.bmp
if (ErrorLevel !=0)
{
msgbox, Logo Input Error
return
}
else
{
library.pressMatrixButton("LogoOpen")
library.randomWait()
mousemove, 0, 0
sleep, 100
}
}
library.pressMatrixButton("LogoClosed")
library.randomWait()
If this.isControlled
{
Send, {Tab 2}Drugs{Tab}%dose%{Tab}%drug%{Tab 3}Yes
library.randomWait(4)
}
else
{
send, {Tab 2}Drugs{Tab 2}%drug% %dose%{Tab 3}Yes
library.randomWait(4)
}
library.pressMatrixButton("LogoOpen")
library.randomWait()
return
}
putTabletNRIntoMatrix(itemDescription)
{
nNumber := this.nNumber
if (this.isControlled and not RegExMatch(itemDescription, "(piece|obscured)"))
{
rNumber := 1
this.rNumber := 1
if (nNumber > 1)
{
logoCodex := "one"
}
}
if (this.isControlled and RegExMatch(itemDescription, "(piece|obscured)"))
{
rNumber := 1
this.rNumber :=1
logoCodex := ""
}
if (not this.isControlled and RegExMatch(itemDescription, "(piece|obscured)"))
{
rNumber := nNumber - 1
this.rNumber := nNumber - 1
logoCodex := ""
}
if (not this.isControlled and not RegExMatch(itemDescription, "(piece|obscured)"))
{
rNumber := nNumber
this.rNumber := nNumber
logoCodex := ""
}
ImageSearch, x, y, 0, -240, 2499, 1439, %A_ScriptDir%/Img/Matrix/NRClosed.bmp
if (ErrorLevel != 0)
{
ImageSearch, x, y, 0, -240, 2499, 1439, %A_ScriptDir%/Img/Matrix/NROpen.bmp
if (ErrorLevel !=0)
{
msgbox, N and R Input Error
return
}
else
{
library.pressMatrixButton("NROpen")
library.randomWait()
mousemove, 0, 0
sleep, 100
}
}
library.pressMatrixButton("NRClosed")
library.randomWait()
send, {TAB}%nNumber%{TAB}%rNumber%{TAB}%logoCodex%
library.randomWait(2)
mousemove, 0, 0
sleep, 100
library.pressMatrixButton("NROpen")
library.randomWait()
return
}
moveToWeight(itemDescription)
{
library.pressMatrixButton("ReportOpen")
library.randomWait()
library.pressMatrixButton("PackagingOpen")
library.randomWait()
if (not this.isControlled)
{
weightType := ""
}
else if (this.rNumber = 1 and this.nNumber > 1 and this.isControlled)
{
weightType := "population"
}
else if (this.rNumber = 1 and this.nNumber = 1 and this.isControlled)
{
weightType := "net"
}
if (weightType)
{
library.pressMatrixButton("WeightClosed")
library.randomWait()
ImageSearch, x, y, 0, -240, 2499, 1439, %A_ScriptDir%/Img/Matrix/NetWeight.bmp
if (ErrorLevel = 0)
{
x := x+44
y := y+25
Click, %x%, %y%
sleep, 250
}
}
return
}
longNumberToShortNumber(text) ; Limits its search to the first words of a string, also only looks for numbers of 1 to 999
{
hundreds := {1:"one", 2:"two", 3:"three", 4:"four", 5:"five", 6:"six", 7:"seven", 8:"eight", 9:"nine"}
singles := {1:"one", 2:"two", 3:"three", 4:"four", 5:"five", 6:"six", 7:"seven", 8:"eight", 9:"nine"}
teens := {10:"ten", 11:"eleven", 12:"twelve", 13:"thirteen", 14:"fourteen", 15:"fifteen", 16:"sixteen", 17:"seventeen", 18:"eighteen", 19:"nineteen"}
tens := {20:"twenty", 30:"thirty", 40:"forty", 50:"fifty", 60:"sixty", 70:"seventy", 80:"eighty", 90:"ninety"}
If (InStr(text, "hundred"))
{
hh := ""
for k,v in hundreds
{
if (RegExMatch(text, "i)^" . v))
{
h := k
hh := v . " hundred "
break
}
}
}
tt := ""
t := 0
for k,v in tens
{
if (RegExMatch(text, "i)^" . hh . v))
{
t := k
tt := v
if (RegExMatch(text, "i)^" . hh . tt . "-"))
{
tt := tt . "-"
t := round((k/10))
}
else
{
return h . t
}
}
}
if (not tt)
{
for k,v in teens
{
if (RegExMatch(text, "i)^" . hh . v))
{
teen := k
return h . teen
}
}
for k,v in singles
{
if (RegExMatch(text, "i)^" . hh . v))
{
s := k
return h . s
}
}
}
if (InStr(tt, "-"))
{
for k,v in singles
{
if (RegExMatch(text, "i)^" . hh . tt . v))
{
s := k
return h . t . s
}
}
}
return msgbox, Number Convertor for N number did failed
}
; **********************************************************************************************************************************************
;************Pointer to Open IE Window******************
WBGet(WinTitle="ahk_class IEFrame", Svr#=1) ;// based on ComObjQuery docs
{ static msg := DllCall("RegisterWindowMessage", "str", "WM_HTML_GETOBJECT")
, IID := "{0002DF05-0000-0000-C000-000000000046}" ;// IID_IWebBrowserApp
;// , IID := "{332C4427-26CB-11D0-B483-00C04FD90119}" ;// IID_IHTMLWindow2
SendMessage msg, 0, 0, Internet Explorer_Server%Svr#%, %WinTitle%
if (ErrorLevel != "FAIL") {
lResult:=ErrorLevel, VarSetCapacity(GUID,16,0)
if DllCall("ole32\CLSIDFromString", "wstr","{332C4425-26CB-11D0-B483-00C04FD90119}", "ptr",&GUID) >= 0 {
DllCall("oleacc\ObjectFromLresult", "ptr",lResult, "ptr",&GUID, "ptr",0, "ptr*",pdoc)
return ComObj(9,ComObjQuery(pdoc,IID,IID),1), ObjRelease(pdoc)
}
}
}
ClickLink(PXL,Text="")
{
ComObjError(false)
Links := PXL.Document.Links
Loop % Links.Length
If (Links[A_Index-1].InnerText = Text ) { ; search for Text
Links[A_Index-1].Click() ;click it when you find it
break
}
ComObjError(True)
}
}