Michael Honey
New Member
- Outlook version
- Outlook 2013 64 bit
- Email Account
- Exchange Server
Hi guys,
I have a macro below which works perfectly fine, but only my the default mailbox (Mailbox - Michael Honey). It automatically prints attachments as mail arrives into my inbox that have a pdf attachment only.
I understand the macro works, but just need to know how to access another mailbox (2nd mailbox called JMAP Remittance). I have more than one mailbox in my Outlook.
How can I change the Ns Object to look at a different mailbox other than the default.
I can do things like msgbox Ns.folders(1).Name and it shows the JMAP Remittance mailbox name, but I can't set it as an object so that I can access all the email Items.
If you can offer some advice as how I can set the object to the other mailbox account I would greatly appreciate it.
Thanks.
Michael.
Code Below:
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
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Dim Ns As Outlook.NameSpace
Dim Folder As Outlook.MAPIFolder
Set Ns = Application.GetNamespace("MAPI")
Set Folder = Ns.GetDefaultFolder(olFolderInbox)
Set Items = Folder.Items
End Sub
Private Sub Items_ItemAdd(ByVal Item As Object)
If TypeOf Item Is Outlook.MailItem Then
PrintAttachments Item
MovePrintedMail Item
End If
End Sub
Private Sub PrintAttachments(oMail As Outlook.MailItem)
On Error Resume Next
Dim colAtts As Outlook.Attachments
Dim oAtt As Outlook.Attachment
Dim sFile As String
Dim sDirectory As String
Dim sFileType As String
sDirectory = "c:\Attachments\"
Set colAtts = oMail.Attachments
If colAtts.Count Then
For Each oAtt In colAtts
' This code looks at the last 4 characters in a filename
sFileType = LCase$(Right$(oAtt.FileName, 4))
Select Case sFileType
' Add additional file types below
Case ".pdf"
sFile = sDirectory & oAtt.FileName
oAtt.SaveAsFile sFile
ShellExecute 0, "print", sFile, vbNullString, vbNullString, 0
End Select
Next
End If
End Sub
Sub MovePrintedMail(oMail As Outlook.MailItem)
Dim objDestFolder As Outlook.MAPIFolder
Set objDestFolder = Session.Folders("Mailbox - Michael Honey").Folders("Inbox").Folders("Printed")
oMail.Move objDestFolder
Set objDestFolder = Nothing
End Sub
I have a macro below which works perfectly fine, but only my the default mailbox (Mailbox - Michael Honey). It automatically prints attachments as mail arrives into my inbox that have a pdf attachment only.
I understand the macro works, but just need to know how to access another mailbox (2nd mailbox called JMAP Remittance). I have more than one mailbox in my Outlook.
How can I change the Ns Object to look at a different mailbox other than the default.
I can do things like msgbox Ns.folders(1).Name and it shows the JMAP Remittance mailbox name, but I can't set it as an object so that I can access all the email Items.
If you can offer some advice as how I can set the object to the other mailbox account I would greatly appreciate it.
Thanks.
Michael.
Code Below:
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
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Dim Ns As Outlook.NameSpace
Dim Folder As Outlook.MAPIFolder
Set Ns = Application.GetNamespace("MAPI")
Set Folder = Ns.GetDefaultFolder(olFolderInbox)
Set Items = Folder.Items
End Sub
Private Sub Items_ItemAdd(ByVal Item As Object)
If TypeOf Item Is Outlook.MailItem Then
PrintAttachments Item
MovePrintedMail Item
End If
End Sub
Private Sub PrintAttachments(oMail As Outlook.MailItem)
On Error Resume Next
Dim colAtts As Outlook.Attachments
Dim oAtt As Outlook.Attachment
Dim sFile As String
Dim sDirectory As String
Dim sFileType As String
sDirectory = "c:\Attachments\"
Set colAtts = oMail.Attachments
If colAtts.Count Then
For Each oAtt In colAtts
' This code looks at the last 4 characters in a filename
sFileType = LCase$(Right$(oAtt.FileName, 4))
Select Case sFileType
' Add additional file types below
Case ".pdf"
sFile = sDirectory & oAtt.FileName
oAtt.SaveAsFile sFile
ShellExecute 0, "print", sFile, vbNullString, vbNullString, 0
End Select
Next
End If
End Sub
Sub MovePrintedMail(oMail As Outlook.MailItem)
Dim objDestFolder As Outlook.MAPIFolder
Set objDestFolder = Session.Folders("Mailbox - Michael Honey").Folders("Inbox").Folders("Printed")
oMail.Move objDestFolder
Set objDestFolder = Nothing
End Sub