I have VBA code to look for attachments in e-mails in a specific Outlook folder and save them into a location.
I have an Outlook rule that sends the emails to the Outlook folder then fires the macro. The code does not fire on my computer automatically, but it does on another computer. The email comes in and the rule drops it into the folder, but the code does not run. When I run the code manually, through the VB Editor, it saves the attachment in the right place.
I have checked to make sure folder and path names are correct (either way if they were wrong the code would not run properly when I run it manually).
Outlook rule:
Apply this rule after the message arrives
from xxxxxxxxxxxx
and with xxxx in the subject
and on this machine only
move it to the xxxxx folder
and run Project1.ThisOutlookSession.Save
VBA code:
Sub Save(item As Outlook.MailItem)
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim olFolder As Outlook.MAPIFolder
Dim msg As Outlook.MailItem
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
Set olFolder = olFolder.Folders("xxxxxxx")
Set oApp = CreateObject("Shell.Application")
For Each myitem In olFolder.Items
For Each att In myitem.Attachments
att.SaveAsFile "\\.............\" & att.FileName
myitem.Attachments.Remove 1
myitem.Save
Next
Next
End Sub
The incoming mailitem is item in Sub Save(item As Outlook.MailItem). It is not used in the code.
Later when run manually the code still ignores whatever item is passed but all items in olFolder are now processed.
Try this:
Sub Save(item As Outlook.MailItem)
For Each att In item.Attachments
att.SaveAsFile "\\.............\" & att.FileName
item.Attachments.Remove 1
item.Save
Next
End Sub
try deleting the file (save the codes somewhere else first) ... \Documents and Settings\\Application Data\Microsoft\Outlook\VbaProject.OTM
Related
I have Outlook code which checks specific subject email in shared mailbox (Inbox) and records the email body data in Excel (yet to add code) and should move the email to MIAL folder.
I get an error while moving emails to MIAL folder.
"Variable not defined"
Folder (MIAL) was created manually. When I code to move emails to default folder like "Drafts" or "Sent Folder" its working.
Option Explicit
Public Sub Example()
Dim olNs As Outlook.NameSpace
Set olNs = Application.GetNamespace("MAPI")
Dim Recip As Outlook.Recipient
Set Recip = olNs.CreateRecipient("Import-BOM#email.com") 'update email
Dim SharedInbox As Outlook.Folder
Set SharedInbox = olNs.GetSharedDefaultFolder(Recip, _
olFolderInbox) 'Inbox
Dim Movefolder As Outlook.Folder
Set Movefolder = olNs.GetSharedDefaultFolder(Recip, _
olFolderMIAL) 'Folder
Dim Item As Outlook.MailItem
For Each Item In SharedInbox.Items
'If (Item.subject = "TSP") Then
Debug.Print Item.subject
Item.Move Movefolder
'End If
Next
End Sub
There are specific default folders that can be described in a similar manner to olFolderInbox to be used as shortcuts.
Manually created folders are referenced the long way.
One way to reference a folder at the same level as an inbox is to navigate the folder tree from that inbox to the mailbox then back down again.
Set Movefolder = SharedInbox.Parent.Folders("MIAL")
Move Folder should be
Dim Movefolder As Outlook.folder
Set Movefolder = SharedInbox.Folders("MIAL")
An outlook mailitem is on a network share, and is prone to conflicts especially when modifications are made to it.
The following VBA macro does the following:
Makes changes to a mailitem
Checks if the mailitem is saved and saves it.
Checks the saved mailitem for any conflicts.
How do I modify this code to prevent the mailitem from entering into a conflict in the first place?
Any ideas a welcome.
Sub CheckConflict()
Dim olApp As Outlook.Application
Dim objMail As Outlook.MailItem
Set olApp = Outlook.Application
Set objMail = olApp.ActiveInspector.CurrentItem
objMail.Subject = "Changing subject and saving mail"
If objMail.Saved = False Then
objMail.Save
End if
If objMail.IsConflict = True Then
Msgbox "Conflict detected!"
End If
Set olApp = Nothing
Set objMail = Nothing
End Sub
Minimize the number of times you call Save and do not keep the MailItem object open for any prolonged periods of time.
I made a code that would take an incomming email in a specific folder (First a rule is created in order to move the mail to the folder and then the script is launched).
The problem is that the rule is working (it moves the mail to the folder), but the script isn't.
The thing is that when I take the new mail and transfer it to myself (My email is also in the receivers in the rules), the script is correctly working.
Here is the beginning of the code that I believe may be wrong.
Sub Script(item As Outlook.MailItem)
Dim strMailID As String
Dim objMail As Outlook.MailItem
Dim objNamespace As Outlook.NameSpace
strMailID = item.EntryID
Set objNamespace = Application.GetNamespace("MAPI")
Set objMail = objNamespace.GetItemFromID(strMailID)
Dim objpf As MAPIFolder
If objMail.MessageClass = "IPM.Note" Then
Any help would be appreciated
You need add an event listener to the default local Inbox, it worked with Outlook 2016.
This code will add an event listener to the default local Inbox. Action will be placed upon incoming emails. You need to add actions you need in the code below:
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
' default local Inbox
Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub Items_ItemAdd(ByVal item As Object)
On Error Goto ErrorHandler
Dim Msg As Outlook.MailItem
If TypeName(item) = "MailItem" Then
Set Msg = item
' ******************
' do something here
' ******************
End If
ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ProgramExit
End Sub
After pasting the code in ThisOutlookSession module, you must restart Outlook.
My Outlook macro worked at one time, then stopped. When it parses, the macro throws an error "Compile Error: Variable not defined". It seems the macro does not recognize that that folder exists. I cut the code to the bare minimum and it is a repeatable problem. The macro will recognize standard folders such as JUNK and DRAFTS but not PROCESSED_FOLDERS. I have tried renaming Processed_Folders as well as creating a new folder with a different name. No joy.
Folder structure is:
reports#xxx.com
Inbox
Drafts
Sent
Trash
Junk
Processed_Reports
Outbox
Sync Issues1 (This computer only)
SearchFolders
CODE:
Sub testfforfolder()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim olFolder As Outlook.MAPIFolder
Dim msg As Outlook.MailItem
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
On Error GoTo xyz
Set olFolder = olFolder.Folders("Processed_Reports")
MsgBox "Folder Exists" ' This line works if I use DRAFTS or JUNK
Exit Sub
xyz:
MsgBox ("Cannot find Folder") ' I get here if I use PROCESSED_REPORTS
Exit Sub
End Sub
Thanks to the comment by Tony Dallimore I was able to solve the issue. The link to https://stackoverflow.com/a/12146315/973283 let me solve the problem. With an updated version of Outlook, the default email account was being referenced rather than the account of the selected item. The Processed_Reports folder only existed in a different account folder. The solution, as Tony suggested, was to set the target folder to the full path to the target. I did need one more level as shown in the working solution below.
Sub testfforfolder()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim olFolder As Outlook.MAPIFolder
Dim msg As Outlook.MailItem
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
On Error GoTo xyz
'OLD INCORRECT
'Set olFolder = olFolder.Folders("Processed_Reports")
'WORKING CORRECTION
Set TgtFolder= _
Session.Folders("reports#xxx.com"). _
Folders("Inbox").Folders("Processed_Reports")
MsgBox "Folder Exists" ' This line works if I use DRAFTS or JUNK
Exit Sub
xyz:
MsgBox ("Cannot find Folder") ' I get here if I use PROCESSED_REPORTS
Exit Sub
End Sub
here is a way to get the session folder without knowing the session name
Sub topFolder()
Dim topFolder As Folder
Set topFolder = Application.Session.GetDefaultFolder(olFolderInbox).Parent
Dim i As Integer
For i = 1 To topFolder.Folders.Count
Debug.Print topFolder.Folders(i).Name
Next i
For i = 1 To topFolder.Folders("inbox").Folders.Count
Debug.Print topFolder.Folders("inbox").Folders(i).Name
Next i
End Sub
I sometimes get emails that I want to keep but to move them into the appropriate folder can be a pain. How can I execute a script that will move (like using C-S-v) the email I'm looking at into a certain folder called "buffer", for instance?
I'm using Outlook 2007.
thanks.
EDIT:
there isn't any criteria that can be created to automate this process like through a rule. it is merely a judgment call I make as i'm staring at it.
This code may work better.
In your code, objFolder may be equal to Nothing, yet you continue the procedure. Also, the For Each loop assumes that each item is a mail item.
Sub MoveSelectedMessagesToFolder()
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim obj As Object
Dim msg As Outlook.mailItem
Set objNS = Application.GetNamespace("MAPI")
On Error Resume Next
Set objFolder = objNS.Folders.item("Personal Folders").Folders.item("Buffer")
On Error GoTo 0
If objFolder Is Nothing Then
MsgBox "This folder doesn't exist!", vbOKOnly + vbExclamation, "INVALID FOLDER"
Exit Sub
End If
For Each obj In ActiveExplorer.Selection
If TypeName(obj) = "MailItem" Then
Set msg = obj
msg.Move objFolder
End If
Next obj
End Sub
Here's the code I'm using.
Sub MoveSelectedMessagesToFolder()
'Originally written by Chewy Chong
'Taken from http://verychewy.com/archive/2006/04/12/outlook-macro-to-move-an-email-to-folder.aspx
'Thanks Chewy!
'Ken
On Error Resume Next
Dim objFolder As Outlook.MAPIFolder, objInbox As Outlook.MAPIFolder
Dim objNS As Outlook.NameSpace, objItem As Outlook.MailItem
Set objNS = Application.GetNamespace("MAPI")
Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
'For the "Item" portion, I used the name of the folder exactly as it appear in the ToolTip when I hover over it.
Set objFolder = objNS.Folders.Item("Personal Folders").Folders.Item("Buffer")
'Assume this is a mail folder
If objFolder Is Nothing Then
MsgBox "This folder doesn't exist!", vbOKOnly + vbExclamation, "INVALID FOLDER"
End If
If Application.ActiveExplorer.Selection.Count = 0 Then
'Require that this procedure be called only when a message is selected
Exit Sub
End If
For Each objItem In Application.ActiveExplorer.Selection
If objFolder.DefaultItemType = olMailItem Then
If objItem.Class = olMail Then
objItem.Move objFolder
End If
End If
Next
Tools -> Rules & Alerts
Then Create a new rule telling all mail that fit whatever criteria to be deleted/marked as read/moved to a folder/any combination of those.
Edit:
If you don't want a rule/can't make a rule that fits, you can create a Macro (Tools -> Macro) to move it to a folder, then bind it to a shortcut.