Where do I place this code - WithEvents??

  • Thread starter Thread starter Patrick Graham
  • Start date Start date
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
 
Ok figured that bit out, I think it goes in the
CurrentOutlookSession folder.

But how do I get the code to run, I thought it would
happen when I received and email or start OUtlook but
nothing appears to happen. Do I need to do something else?
 
Seems the work security level is set to Custom. I'm not
sure I want to change too much buy going to lower or
higher.

What specificly in the custom security breakdown do I
need to enable or disable. Is it a script security
setting?
 
In Outlook, Tools | Macro | Security. Your custom IE security settings are
not relevant.
 
Sorry Im not speaking of the IE, the Outlook Security
Settings are custom, not High Low or Medium. And within
the custome settings there is a script category, is this
the right area to enable? And if so which of the sub
categories of the script do I need?

Sorry normally I would just change and test stuff but Im
busy busy busy
 
Not the Outlook *macro* security settings. There is no custom macro security
setting.
 
Please read my earlier message again. Tools | Macro | Security is what you
need to be looking at.
 
Back
Top