Using ItemAdd to work with new emals.

Y

Yonah Sudwerts

Here is my procedure ..

Private Sub myOlItems_ItemAdd(ByVal Item As Object)
Dim objMsg As Object, objAttachments As Outlook.Attachments
For Each objMsg In myOlItems

Set objAttachments = objMsg.Attachments
lngCount = objAttachments.Count
If lngCount > 0 Then
For i = lngCount To 1 Step -1
strFile = "C:\" & objAttachments.Item(i).FileName
objAttachments.Item(i).SaveAsFile strFile
Next i
End If
objMsg.Save
Next
End Sub

I am trying to handle each Email as it arrives, but obviously, if you can
notice the problem is that it runs on the entire Inbox Folder. My
understanding is that the Procedure also receives the ID number [ The (ByVal
Item As Object)part]. That is what I understand. Now, How would I go about
using that ID to access only that email?

Yes, the myOlItems object is initialized, if I stick some simple Msgbox in
here instead of all my code, it works, so I know that the procedure is
responding when a new email arrives.

Can someone please help me out?

Thanks,

Yoni
 
S

Sue Mosher [MVP-Outlook]

Instead of processing every item in the folder every time, process just the
Item that was added:

Set objAttachments = Item.Attachments

etc.
 
Y

Yonah Sudwerts

Thanks, but that part I got. (I am getting there). Here is what I have:

Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Dim myOlApp As New Outlook.Application
Public WithEvents myOlItems As Outlook.Items

Public Sub Initialize_handler()
Set myOlItems =
myOlApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub myOlItems_ItemAdd(ByVal Item As Object)
Dim count As Integer
Dim lRetVal As Long
Set myAtt = Item.Attachments
count = myAtt.count
If count > 0 Then
strFile = "C:\" & myAtt.Item(1).FileName
myAtt.Item(1).SaveAsFile strFile
lRetVal = ShellExecute(0&, "print", strFile, 0&, 0&, 0&)
End If
End Sub
<<<<
Its sitting in the This Outlook Session. What can I do to replace the
Initialisizing part? Right now I have to run it manually for my script to
work. How can I get it to initialize automatically?

Obviously, I still have to work on some Ifs to control which emails get
processed, but I got the big part done.
Also, is there a simple way to erase the file that I created in my script? I
don't need the record of the files as I will keep the stuff in Outlook.
(Though I am contemplating, keeping the files and not the Emails. (We'll
see, I have to talk to the person I am working for)

OK, Those seem to be my 2 questions for now.
Thanks for your speedy reply.

Yoni (e-mail address removed)



Sue Mosher said:
Instead of processing every item in the folder every time, process just
the Item that was added:

Set objAttachments = Item.Attachments

etc.
--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



Yonah Sudwerts said:
Here is my procedure ..

Private Sub myOlItems_ItemAdd(ByVal Item As Object)
Dim objMsg As Object, objAttachments As Outlook.Attachments
For Each objMsg In myOlItems

Set objAttachments = objMsg.Attachments
lngCount = objAttachments.Count
If lngCount > 0 Then
For i = lngCount To 1 Step -1
strFile = "C:\" & objAttachments.Item(i).FileName
objAttachments.Item(i).SaveAsFile strFile
Next i
End If
objMsg.Save
Next
End Sub

I am trying to handle each Email as it arrives, but obviously, if you can
notice the problem is that it runs on the entire Inbox Folder. My
understanding is that the Procedure also receives the ID number [ The
(ByVal Item As Object)part]. That is what I understand. Now, How would I
go about using that ID to access only that email?

Yes, the myOlItems object is initialized, if I stick some simple Msgbox
in here instead of all my code, it works, so I know that the procedure is
responding when a new email arrives.

Can someone please help me out?

Thanks,

Yoni
 
S

Sue Mosher [MVP-Outlook]

Put the code in Initialize_handler in the Application_Startup event handler.
Do not Dim a New Outlook.Application, but use the intrinsic Application
object available to you in ThisOutlookSession.

You can use the Kill method to delete the file. It takes the full path as an
argument:

Kill strFile

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



Yonah Sudwerts said:
Thanks, but that part I got. (I am getting there). Here is what I have:

Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Dim myOlApp As New Outlook.Application
Public WithEvents myOlItems As Outlook.Items

Public Sub Initialize_handler()
Set myOlItems =
myOlApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub myOlItems_ItemAdd(ByVal Item As Object)
Dim count As Integer
Dim lRetVal As Long
Set myAtt = Item.Attachments
count = myAtt.count
If count > 0 Then
strFile = "C:\" & myAtt.Item(1).FileName
myAtt.Item(1).SaveAsFile strFile
lRetVal = ShellExecute(0&, "print", strFile, 0&, 0&, 0&)
End If
End Sub
<<<<
Its sitting in the This Outlook Session. What can I do to replace the
Initialisizing part? Right now I have to run it manually for my script to
work. How can I get it to initialize automatically?

Obviously, I still have to work on some Ifs to control which emails get
processed, but I got the big part done.
Also, is there a simple way to erase the file that I created in my script?
I
don't need the record of the files as I will keep the stuff in Outlook.
(Though I am contemplating, keeping the files and not the Emails. (We'll
see, I have to talk to the person I am working for)

OK, Those seem to be my 2 questions for now.
Thanks for your speedy reply.

Yoni (e-mail address removed)



Sue Mosher said:
Instead of processing every item in the folder every time, process just
the Item that was added:

Set objAttachments = Item.Attachments

etc.
--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



Yonah Sudwerts said:
Here is my procedure ..

Private Sub myOlItems_ItemAdd(ByVal Item As Object)
Dim objMsg As Object, objAttachments As Outlook.Attachments
For Each objMsg In myOlItems

Set objAttachments = objMsg.Attachments
lngCount = objAttachments.Count
If lngCount > 0 Then
For i = lngCount To 1 Step -1
strFile = "C:\" & objAttachments.Item(i).FileName
objAttachments.Item(i).SaveAsFile strFile
Next i
End If
objMsg.Save
Next
End Sub

I am trying to handle each Email as it arrives, but obviously, if you
can
notice the problem is that it runs on the entire Inbox Folder. My
understanding is that the Procedure also receives the ID number [ The
(ByVal Item As Object)part]. That is what I understand. Now, How would I
go about using that ID to access only that email?

Yes, the myOlItems object is initialized, if I stick some simple Msgbox
in here instead of all my code, it works, so I know that the procedure
is
responding when a new email arrives.

Can someone please help me out?

Thanks,

Yoni
 
Y

Yonah Sudwerts

That sounds great, but I have no clue how to implement it ;)
You might showing me a little deeper? Thanks

And I tried using the Kill StrFile, and it seems to work too fast, it Kills
it before the print command has a chance to work. ??

Thanks for your quick replies.
Yoni


Sue Mosher said:
Put the code in Initialize_handler in the Application_Startup event
handler. Do not Dim a New Outlook.Application, but use the intrinsic
Application object available to you in ThisOutlookSession.

You can use the Kill method to delete the file. It takes the full path as
an argument:

Kill strFile

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



Yonah Sudwerts said:
Thanks, but that part I got. (I am getting there). Here is what I have:

Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String,
_
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Dim myOlApp As New Outlook.Application
Public WithEvents myOlItems As Outlook.Items

Public Sub Initialize_handler()
Set myOlItems =
myOlApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub myOlItems_ItemAdd(ByVal Item As Object)
Dim count As Integer
Dim lRetVal As Long
Set myAtt = Item.Attachments
count = myAtt.count
If count > 0 Then
strFile = "C:\" & myAtt.Item(1).FileName
myAtt.Item(1).SaveAsFile strFile
lRetVal = ShellExecute(0&, "print", strFile, 0&, 0&, 0&)
End If
End Sub
<<<<
Its sitting in the This Outlook Session. What can I do to replace the
Initialisizing part? Right now I have to run it manually for my script to
work. How can I get it to initialize automatically?

Obviously, I still have to work on some Ifs to control which emails get
processed, but I got the big part done.
Also, is there a simple way to erase the file that I created in my
script? I
don't need the record of the files as I will keep the stuff in Outlook.
(Though I am contemplating, keeping the files and not the Emails. (We'll
see, I have to talk to the person I am working for)

OK, Those seem to be my 2 questions for now.
Thanks for your speedy reply.

Yoni (e-mail address removed)



Sue Mosher said:
Instead of processing every item in the folder every time, process just
the Item that was added:

Set objAttachments = Item.Attachments

etc.
--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



Here is my procedure ..

Private Sub myOlItems_ItemAdd(ByVal Item As Object)
Dim objMsg As Object, objAttachments As Outlook.Attachments
For Each objMsg In myOlItems

Set objAttachments = objMsg.Attachments
lngCount = objAttachments.Count
If lngCount > 0 Then
For i = lngCount To 1 Step -1
strFile = "C:\" & objAttachments.Item(i).FileName
objAttachments.Item(i).SaveAsFile strFile
Next i
End If
objMsg.Save
Next
End Sub

I am trying to handle each Email as it arrives, but obviously, if you
can
notice the problem is that it runs on the entire Inbox Folder. My
understanding is that the Procedure also receives the ID number [ The
(ByVal Item As Object)part]. That is what I understand. Now, How would
I
go about using that ID to access only that email?

Yes, the myOlItems object is initialized, if I stick some simple Msgbox
in here instead of all my code, it works, so I know that the procedure
is
responding when a new email arrives.

Can someone please help me out?

Thanks,

Yoni
 
S

Sue Mosher [MVP-Outlook]

How closely did you look at the ThisOutlookSession module? I think it's kind
of hard to miss the fact that there are two drop-down lists at the top. If
you choose Application, you see a list of Application events, including
StartUp. If you choose Startup, it automatically inserts the structure for
the Appliation_Startup event handler into the module.

Got it so far?

Now you need to copy the code in the Initialize_handler procedure to the
Application_Startup procedure and use the intrinsic Application object
instead of a new Outlook.Application object. That means that instead of what
you have now

Set myOlItems =
myOlApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items

you would want to use:

Set myOlItems =
Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items

I don't use Shell Execute often enough to know what options you have. Maybe
the lRetVal it returns is signficant and you can test for some completion
value in a Do loop. It may take some experimentation on your part.
--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
K

Ken Slovak - [MVP - Outlook]

lRetVal won't help.

From the MSDN library entry for ShellExecute:
In many situations, you may want to launch an application program and force
the operating system to wait until that application has been terminated
before allowing any other action to be performed. Unfortunately, you cannot
use the ShellExecute function to do this!

You must use several other Windows API functions to launch and wait for the
program to be terminated. The ExecuteAndWait method, shown below, can
address this problem quite nicely. All you have to do is provide the full
pathname and its required arguments to this method. The application you have
just launched will retain the focus until it is subsequently terminated.

Public Sub ExecuteAndWait(cmdline As String)
Dim NameOfProc As PROCESS_INFORMATION
Dim NameStart As STARTUPINFO
Dim X As Long
NameStart.cb = Len(NameStart)
X = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&,
0&, NameStart, NameOfProc)
X = WaitForSingleObject(NameOfProc.hProcess, INFINITE)
X = CloseHandle(NameOfProc.hProcess)
End Sub
As you can see from the code above, the ExecuteAndWait method uses several
functions-CreateProcessA, WaitForSingleObject, and CloseHandle. We have
already seen that a Windows application program can be executed by calling
the ShellExecute function. You can also use the CreateProcessA function to
launch applications and to force that application to retain the focus until
it is terminated
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top