What is wrong with this code?

M

Mota

Hello;
I copied this code from the NewsGroup(thanx to its provider),and in
Microsoft outlook,double clicked on "ThisOutlookSession",and pasted it in
the opening module.So i expected when a mail having an Attachment
receives,automatically be saved in the StrPath,difined in SaveAttachment
function as its argument.
But when a mail arrives,nothing happens and no error occures.I think the
triggered Event doesnt occure.what i have to do to do so,or which seetings
may be wrong in my MS Outlook?I program in MS Access and familiar with VBA
coding,but not in Outlook Events,Collections and.....
Can anyone please help me?
Thank you in advance.

Private Sub Application_NewMail()
' Occurs when one or more new messages are received in the Inbox
SaveAttachments "E:\PrescPro\ReceivedFiles", "My Virtual Server"
End Sub

Public Sub SaveAttachments(strExportPath As String, strFrom As String)
' Declare objects
Dim olNameSpace As Outlook.NameSpace
Dim olInbox As Outlook.MAPIFolder
Dim olAttach As Outlook.Attachment
Dim olItem As Object

Set olNameSpace = Application.GetNamespace("MAPI")

' Get the default inbox
Set olInbox = olNameSpace.GetDefaultFolder(olFolderInbox)

' Loop on the items collection from the inbox folder
For Each olItem In olInbox.Items
' Check if the item is a mail (could be a task, etc.)
If olItem.Class = olMail Then
' Check the sender name and unread status
' Check if there are attachments
If olItem.Attachments.Count > 0 Then
For Each olAttach In olItem.Attachments
' Save attachments
olAttach.SaveAsFile strExportPath & _
"\" & olAttach.FileName
Next
' Move the item to the deleted folder
olItem.Delete
End If
End If
Next

Set olItem = Nothing
Set olInbox = Nothing
Set olNameSpace = Nothing
End Sub
 
M

Mota

I never changed this setting,and it must be on its default.However,its
better to check it.How i can do this?
In addition,i didnt make a macro.i just double clicked on
"thisOutlookSession" in project explorer.I didnt do enything else and i
blieve that its cause of my problem.It saved as "Project1".Does the Project1
have any link to the Outlook by default?Do i need to do anything else?
Thank you for ur attention.

Ken Slovak - said:
Is your macro security set to High? If it is no macros will run unless
you sign and trust the signature for your VBAProject.OTM file. You can
use the selfcert.exe program that comes with Office to sign your
macros, or you can lower your macro security to Medium.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Lead Author, Professional Outlook 2000 Programming, Wrox Press
Lead Author, Beginning VB 6 Application Development, Wrox Press
Attachment Options
http://www.slovaktech.com/attachmentoptions.htm
Extended Reminders
http://www.slovaktech.com/extendedreminders.htm


Mota said:
Hello;
I copied this code from the NewsGroup(thanx to its provider),and in
Microsoft outlook,double clicked on "ThisOutlookSession",and pasted it in
the opening module.So i expected when a mail having an Attachment
receives,automatically be saved in the StrPath,difined in SaveAttachment
function as its argument.
But when a mail arrives,nothing happens and no error occures.I think the
triggered Event doesnt occure.what i have to do to do so,or which seetings
may be wrong in my MS Outlook?I program in MS Access and familiar with VBA
coding,but not in Outlook Events,Collections and.....
Can anyone please help me?
Thank you in advance.

Private Sub Application_NewMail()
' Occurs when one or more new messages are received in the Inbox
SaveAttachments "E:\PrescPro\ReceivedFiles", "My Virtual Server"
End Sub

Public Sub SaveAttachments(strExportPath As String, strFrom As String)
' Declare objects
Dim olNameSpace As Outlook.NameSpace
Dim olInbox As Outlook.MAPIFolder
Dim olAttach As Outlook.Attachment
Dim olItem As Object

Set olNameSpace = Application.GetNamespace("MAPI")

' Get the default inbox
Set olInbox = olNameSpace.GetDefaultFolder(olFolderInbox)

' Loop on the items collection from the inbox folder
For Each olItem In olInbox.Items
' Check if the item is a mail (could be a task, etc.)
If olItem.Class = olMail Then
' Check the sender name and unread status
' Check if there are attachments
If olItem.Attachments.Count > 0 Then
For Each olAttach In olItem.Attachments
' Save attachments
olAttach.SaveAsFile strExportPath & _
"\" & olAttach.FileName
Next
' Move the item to the deleted folder
olItem.Delete
End If
End If
Next

Set olItem = Nothing
Set olInbox = Nothing
Set olNameSpace = Nothing
End Sub
 
K

Ken Slovak - [MVP - Outlook]

Project1 is VBAProject.OTM in the file system. It's your one and only
Outlook VBA project file. ThisOutlookSession code would be affected by
your macro security settings. With High security only signed trusted
macro code will run. With Medium you have to answer a prompt when
Outlook starts to enable unsigned code. Low will run anything and can
be considered a possible security hazard.

To check macro security: Tools, Macros, Security.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Lead Author, Professional Outlook 2000 Programming, Wrox Press
Lead Author, Beginning VB 6 Application Development, Wrox Press
Attachment Options
http://www.slovaktech.com/attachmentoptions.htm
Extended Reminders
http://www.slovaktech.com/extendedreminders.htm
 

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