Jump to content
Sign in to follow this  
VelvetElvis

ComErrorHandler question

Recommended Posts

VelvetElvis

I'm using this cool function in my app, and I'm getting this error:

--> IE.au3 V2.4-0 Warning from function internal function __IEIsObjType, Cannot register internal error handler, cannot trap COM errors (Use _IEErrorHandlerRegister() to register a user error handler)

What my app does is open IE, calling an .asp page to download a report, then it emails the report. To debug, I have an Exit right after my IE code runs, and before _INetSmtpMailCom() is even called.

Funny thing is, the program works fine, exiting without any obvious errors. Hoping someone can help me out. I'm totally a noob when it comes to error handling. :graduated:

Here's the code as it sits. Note the premature "Exit" before Jos's function.

Thanks!

#include <IE.au3>
#include <file.au3>
#include <date.au3>
#include <array.au3>
 
Opt("WinTitleMatchMode", 2) ; So we can match a partial string anywhere in the title
Opt("WinWaitDelay", 1000) ; Default pause after each successful window-related operation
Opt("SendKeyDelay", 50) ; Pause between sent keystrokes
 
Global $LogFile = "D:\LOG_Corbeil_Email_Reports.txt"
Global $oMyRet[2] ; Req'd for email function
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc") ; Req'd for email function
Global $aAddresses, $aURL[2], $aFilename[2]
 
; Read address file into array
_FileReadToArray(@ScriptDir & "\Corbeil_Report_Addresses.txt", $aAddresses)
; Format yesterday's date for URL
$URLdate = _DateAdd('d', -1, _NowCalcDate())
$URLdate = StringMid($URLdate, 6, 2) & "/" & StringRight($URLdate, 2) & "/" & StringLeft($URLdate, 4)
$aURL[0] = "http://ctweb03/ccc/cdc/report/build_report.asp?lang=e&start_date=" & $URLdate & "&end_date=" & $URLdate
$aURL[1] = "http://ctweb03/ccc/cdc/report/build_report_s.asp?lang=e&start_date=" & $URLdate & "&end_date=" & $URLdate
$aFilename[0] = "Corbeil_Detail_" & StringRight($URLdate, 4) & "-" & StringLeft($URLdate, 2) & "-" & StringMid($URLdate, 4, 2) & ".xls"
$aFilename[1] = "Corbeil_Summary_" & StringRight($URLdate, 4) & "-" & StringLeft($URLdate, 2) & "-" & StringMid($URLdate, 4, 2) & ".xls"
 
For $x = 0 To 1
; ------- Open URL
$oIE = _IECreate($aURL[$x], 0, 1, 0)
If @error Then
  MsgBox(4112, "Corbeil Report Emailer", "IE error opening report " & $aFilename[$x], 4)
  _FileWriteLog($LogFile, "IE error opening report " & $aFilename[$x])
  _IEQuit($oIE)
EndIf
; -------  Wait for download box to appear.  Exit w/error if timeout
If WinWait("File Download", "", 30) = 0 Then
  _IEQuit($oIE)
  MsgBox(0, "", "Error waiting for download window")
EndIf
; -------  Initiate download
If Not WinActive("File Download", "") Then WinActivate("File Download", "")
WinWaitActive("File Download", "")
BlockInput(1) ; Block mouse & keyboard input
Send("s")
; ------- Wait for Save As dialog, pass current folder as beginning of filename
WinWait("Save As", "")
If Not WinActive("Save As", "") Then WinActivate("Save As", "")
WinWaitActive("Save As", "")
; ------- Assign filename
Send(@ScriptDir & "\" & $aFilename[$x])
Sleep(300)
Send("!s")
 
Exit  ;<<<<-------------------------------------
 
; *************** email variables
$SmtpServer = "mail"
$FromName = "Methods_&_Systems"
$FromAddress = "noreply@sears.ca"
$ToAddress = $aAddresses[$x]
$Subject = $aFilename[$x]
$Body = "" ;
$AttachFiles = @ScriptDir & "\" & $aFilename[$x]
$CcAddress = ""
$BccAddress = ""
$Importance = "Normal" ; Send message priority: "High", "Normal", "Low"
$Username = "null" ; username for the account used from where the mail gets sent - REQUIRED
$Password = "null" ; password for the account used from where the mail gets sent - REQUIRED
$IPPort = 25 ; port used for sending the mail
$ssl = 0 ; enables/disables secure socket layer sending - put to 1 if using httpS
$rc = _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl)
If @error Then
  MsgBox(4112, "Corbeil Report Emailer", "Email function failure: " & $rc, 4)
  _FileWriteLog($LogFile, "Email function failure on report " & $aFilename[$x] & "... Error: " & $rc)
  ;MsgBox(0, "", $rc)
  ;if $rc <> 2 then MsgBox(0, "Error sending message", "Error code:" & @error & "  Description:" & $rc)
Else
  _FileWriteLog($LogFile, "Emailed report " & $aFilename[$x])
  ;MsgBox(0, "", "Email sent successfully")
EndIf
Next
;*****************************************************************************
Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Importance = "Normal", $s_Username = "", $s_Password = "", $IPPort = 25, $ssl = 0)
Local $objEmail = ObjCreate("CDO.Message")
$objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>'
$objEmail.To = $s_ToAddress
Local $i_Error = 0
Local $i_Error_desciption = ""
If $s_CcAddress <> "" Then $objEmail.Cc = $s_CcAddress
If $s_BccAddress <> "" Then $objEmail.Bcc = $s_BccAddress
$objEmail.Subject = $s_Subject
If StringInStr($as_Body, "<") And StringInStr($as_Body, ">") Then
  $objEmail.HTMLBody = $as_Body
Else
  $objEmail.Textbody = $as_Body & @CRLF
EndIf
If $s_AttachFiles <> "" Then
  Local $S_Files2Attach = StringSplit($s_AttachFiles, ";")
  For $x = 1 To $S_Files2Attach[0]
   $S_Files2Attach[$x] = _PathFull($S_Files2Attach[$x])
   ;ConsoleWrite('@@ Debug : $S_Files2Attach[$x] = ' & $S_Files2Attach[$x] & @LF & '>Error code: ' & @error & @LF) ;### Debug Console
   If FileExists($S_Files2Attach[$x]) Then
    ;ConsoleWrite('+> File attachment added: ' & $S_Files2Attach[$x] & @LF)
    $objEmail.AddAttachment($S_Files2Attach[$x])
   Else
    ;ConsoleWrite('!> File not found to attach: ' & $S_Files2Attach[$x] & @LF)
    SetError(1)
    Return 0
   EndIf
  Next
EndIf
$objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
$objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $s_SmtpServer
If Number($IPPort) = 0 Then $IPPort = 25
$objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort
;Authenticated SMTP
If $s_Username <> "" Then
  $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
  $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = $s_Username
  $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $s_Password
EndIf
If $ssl Then
  $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
EndIf
;Update settings
$objEmail.Configuration.Fields.Update
; Set Email Importance
Switch $s_Importance
  Case "High"
   $objEmail.Fields.Item("urn:schemas:mailheader:Importance") = "High"
  Case "Normal"
   $objEmail.Fields.Item("urn:schemas:mailheader:Importance") = "Normal"
  Case "Low"
   $objEmail.Fields.Item("urn:schemas:mailheader:Importance") = "Low"
EndSwitch
$objEmail.Fields.Update
; Sent the Message
$objEmail.Send
If @error Then
  SetError(2)
  Return $oMyRet[1]
EndIf
$objEmail = ""
EndFunc   ;==>_INetSmtpMailCom
;
;
;*****************************************************************************
Func MyErrFunc()
; Com Error Handler
$HexNumber = Hex($oMyError.number, 8)
$oMyRet[0] = $HexNumber
$oMyRet[1] = StringStripWS($oMyError.description, 3)
;ConsoleWrite("### COM Error !  Number: " & $HexNumber & "   ScriptLine: " & $oMyError.scriptline & "   Description:" & $oMyRet[1] & @LF)
SetError(1); something to check for when this function returns
Return
EndFunc   ;==>MyErrFunc

Share this post


Link to post
Share on other sites
Jos

The error/warning comes from the IE functions.

You can only have one COmErrorHandler active at a time, so only activate the one for SMTP when you send the Email

Jos

ps: Split of into its own topic.

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
VelvetElvis

Thank you Jos.

Is this what you mean? (my app isn't finished, so I can't test it fully yet, but at least I'm not getting the error.

Global $oMyRet[2] ; Req'd for email function
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc") ; Req'd for email function
 
$rc = _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl)

I guess I'll download the 2 files, then call the email routine in a loop.

My original plan was to download file 1, then email it, and repeat the process for file 2. Can I ask you another question? Been doing some more digging in the help file, and came across _IEErrorHandlerRegister() and _IEErrorHandlerDeRegister()

Would I need to use these functions if I took that approach?

Again, thank you.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×