Error printing html file using IE object model in outlook

  • Thread starter Thread starter Osama Yahya
  • Start date Start date
O

Osama Yahya

Hi there:
I have a macro in outlook that prints html attachment
using IE object model and I am getting ExecWB failed error
when I runs the macro, here is my code, any help is
appreciated.

On Error GoTo GetAttachments_err
Dim Inbox As MAPIFolder
Dim EZBUYSubInbox As MAPIFolder '**
Dim JunkSubInbox As MAPIFolder '**
Dim email As MailItem
Dim Atmt As Attachment
Dim i As Integer, Count As Integer
Const FileName = "C:\ATT\"
Set Inbox = GetNamespace("MAPI").GetDefaultFolder
(olFolderInbox)
Set EZBUYSubInbox = Inbox.Folders("EZBUY")
Set JunkSubInbox = Inbox.Folders("Junk")

Dim IE As New InternetExplorer

i = 0
If Inbox.Items.Count = 0 Then Exit Sub
'cycle through emails
For Each email In Inbox.Items
If email.UnRead Then '**

If email.Subject = "PCO" Then

If email.Attachments.Count > 0 Then
For Count = email.Attachments.Count To 1 Step -1
Set Atmt = email.Attachments.Item(Count)
'If LCase(Right(Atmt.FileName, 3)) = "wav" Or _
' LCase(Right(Atmt.FileName, 3)) = "xml" Then
Atmt.SaveAsFile (FileName & Atmt.FileName)


IE.Visible = False
IE.navigate FileName & Atmt.FileName

While IE.readyState = 4
DoEvents
Wend

Do Until IE.Busy <> False
DoEvents
Loop

IE.ExecWB 6, 2, 0, 0


If Not EZBUYSubInbox Is Nothing Then
' email.Move EZBUYSubInbox
End If

'Atmt.Delete
i = i + 1
'End If
Next Count
' email.Save
End If

Else
email.Move JunkSubInbox

End If
End If '**
Next email
If i > 0 Then
'MsgBox " " & i & " attached files were found." _
' & vbCrLf & "They have been copied to your work
folder." _
' & vbCrLf & vbCrLf & "Have a nice day.",
vbInformation, "Finished!"
End If
GetAttachments_exit:
Set Atmt = Nothing
Set email = Nothing
Set Inbox = Nothing
Set EZBUYSubInbox = Nothing
Set JunkSubInbox = Nothing
Set IE = Nothing
Exit Sub
GetAttachments_err:
MsgBox "An unexpected error has occurred." _
& vbCrLf & "Please note and report the following
information." _
& vbCrLf & "Macro Name: GetAttachments" _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
, vbCritical, "Error!"
Resume GetAttachments_exit
 
The only problem I see is your declaration of the email variable. Change
that to Dim email As Object. If there are any non-email messages in that
folder, an error will be generated because the items are of a different
class.
 
Hi Eric:
Thanks for your reply, I will try to change the email to
object instead of MailItem....but before I make the
changes I just wanted to mention that When I run the macro
in debuggung mode it works fine, I am having the error
only when I run normaly.
 
Are you saying that when you step through the code, it never reaches
GetAttachments_err? If so, that IS weird.
 
Exactly, when I step through the code it doeen't reach
GetAttachmnet_err and works fine. I made the changes
you've suggested to me and it is doing the same thing!!
 
All I can say is that the first error I got was because I didn't have a
C:\ATT folder. I created that, and the code loops continuously at the While
IE.readyState = 4
section; IE never loads, and my PC goes to 100% CPU utilization and does
nothing - I have to stop the code. Maybe try your code with the Web Browser
Control stuff commented out and see if it behaves the same way.

What error message do you get, BTW? You never did say.
 
Back
Top