P
Patrick Graham
Hi I found some code on the net that might do what I want
it to do.
That is place a received email's attachment in a
directory automattically. But I cant figure out where to
place the code. I tried placing it in Module1 but the 2nd
line gives a compile error saying WithEvents is only
valid in an object module. Where does this code belong?
The Code is:
Option Explicit
Private WithEvents olInboxItems As Items
Private Sub Application_Startup()
Dim objNS As NameSpace
Set objNS = Application.GetNamespace("MAPI")
' instantiate objects declared WithEvents
Set olInboxItems = objNS.GetDefaultFolder
(olFolderInbox).Items
Set objNS = Nothing
End Sub
Private Sub Application_Quit()
' disassociate global objects declared WithEvents
Set olInboxItems = Nothing
End Sub
Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
Dim myAttach As Object
Dim myDiskFolder, myFileName, myFileExt As String
On Error Resume Next
' If Item.Subject = "ROB FILE" And Item.UnRead = "True"
Then
If Item.UnRead = "True" Then
myDiskFolder = "c:\"
myFileName = "MyfileName"
'save each mail attachment
Set myAttach = Item.Attachments
If Not myAttach.Count = 0 Then myAttach.Item
(1).SaveAsFile myDiskFolder & myAttach.Item
(1).DisplayName & Right(myAttach.Item(1).DisplayName, 4)
'cleanup
Set myAttach = Nothing
Set Item = Nothing
End If
End Sub
End Sub
it to do.
That is place a received email's attachment in a
directory automattically. But I cant figure out where to
place the code. I tried placing it in Module1 but the 2nd
line gives a compile error saying WithEvents is only
valid in an object module. Where does this code belong?
The Code is:
Option Explicit
Private WithEvents olInboxItems As Items
Private Sub Application_Startup()
Dim objNS As NameSpace
Set objNS = Application.GetNamespace("MAPI")
' instantiate objects declared WithEvents
Set olInboxItems = objNS.GetDefaultFolder
(olFolderInbox).Items
Set objNS = Nothing
End Sub
Private Sub Application_Quit()
' disassociate global objects declared WithEvents
Set olInboxItems = Nothing
End Sub
Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
Dim myAttach As Object
Dim myDiskFolder, myFileName, myFileExt As String
On Error Resume Next
' If Item.Subject = "ROB FILE" And Item.UnRead = "True"
Then
If Item.UnRead = "True" Then
myDiskFolder = "c:\"
myFileName = "MyfileName"
'save each mail attachment
Set myAttach = Item.Attachments
If Not myAttach.Count = 0 Then myAttach.Item
(1).SaveAsFile myDiskFolder & myAttach.Item
(1).DisplayName & Right(myAttach.Item(1).DisplayName, 4)
'cleanup
Set myAttach = Nothing
Set Item = Nothing
End If
End Sub
End Sub