I would like to create the following rule in Outlook:
whenever comes an email from (1) specific address and (2) with specific subject I want to run a script that moves the email's attachment into the inbox subfolder (let's call it MyFolder).
So just to make it clear: I do not want to save the attachment to hard drive folder.
I can do steps 1 and 2, but I have problems with the code for step 3. Most related questions are dealing with saving attachments to hard drive folders.
I would really appreciate your help!
Dmitri
I found this example at
https://www.extendoffice.com/documents/outlook/3747-outlook-auto-download-save-attachments-to-folder.html
Public Sub SaveAttachmentsToDisk(MItem As Outlook.MailItem)
Dim oAttachment As Outlook.Attachment
Dim sSaveFolder As String
sSaveFolder = "C:\Users\DT168\Documents\outlook-attachments\"
For Each oAttachment In MItem.Attachments
oAttachment.SaveAsFile sSaveFolder & oAttachment.DisplayName
Next
End Sub
Also you can add a Rule (step by step example is shown in the page).
An Outlook folder can't contain files. You need to created a document item if it is an Office document (like Word or Excel). Or you will have to create an Outlook item and add the attached file to it.
You may find the Getting Started with VBA in Outlook 2010 article helpful.
Related
I am wondering if there is a way using VBA/Outlook to look through a number of personal folders (all added to Outlook and called personal folders) and copy the contents of the inboxes (the folder will always be called Inbox) to a single PST/inbox/folder. The number of personal folders would vary depending upon the email search completed (GVault).
Personal Folder - Inbox,
Personal Folder - Inbox,
Personal Folder - inbox,
Final Personal Folder - Inbox
The aim is to give the user one PST with all the emails in.
This is part of an attempt to streamline our email archive search process which creates a folder + PST for each email address found in the search (good old Google....) It is obviously a nightmare combining them all into one PST which we can then give to a user. It is possible (using Outlook) to manually combine each PST with a master PST but this is far from automating the process + there could be a large amount of separate email addresses.
The original problem was taking all the PSTs and getting them into Outlook, this has been solved but the format is as described above (seperate PSTs all added).
Any help would be greatly appreciated as I cant get past this final hurdle, there are scripts that do manipulate PSTs in Outlook, they just dont do this.
Thanks
Dan
Below code will loop your outlook attached PST files copying files in a folder called 'Inbox' (any case) to the PST callled 'Master PST'
Included very trivial error checking <-- free to improve
'include reference to Microsoft Outlook XX.0 Object Library
Public Sub copyInbox()
Dim ns As Outlook.Namespace
On Error GoTo hell
Dim sourceFolder As Outlook.MAPIFolder
Dim copyToFolder As Outlook.MAPIFolder
Dim subfolder As Outlook.MAPIFolder
Dim objItem As Outlook.MailItem
Dim objItemCopy As Outlook.MailItem
Set ns = Application.GetNamespace("MAPI")
Set copyToFolder = ns.Folders("MASTER PST").Folders("Inbox")
'Personal folder called 'MASTER PST' with inbox subfolder must exist
For Each sourceFolder In ns.Folders
For Each subfolder In sourceFolder.Folders
If Trim(UCase(subfolder.Name)) = "INBOX" Then
For Each objItem In subfolder.Items
'modified below to use copy as per MSDN
'https://msdn.microsoft.com/en-us/vba/outlook-vba/articles/mailitem-copy-method-outlook
Set objItemCopy = objItem.Copy
objItemCopy.Move copyToFolder
Next
End If
'copy items in subfolders in inbox can be added here
Next
Next
Exit Sub:
hell:
MsgBox Err.Description, vbExclamation, Err.Source
End Sub
I have adjusted a routine found on the internet in Outlook VBA that moves all emails from a conversation in inbox to a specific folder.
I move my emails by getting them like:
olItem As MailItem 'Put email from conversation in olItem
DestFolder As Outlook.Folder 'Destination folder where i want to send my email
olItem.Move DestFolder
Problem is: in this conversation I have sometimes older emails that have already been moved to the destination folder earlier: they appear in my inbox because of the way conversation mode works.
If I try to move it with olItem.Move DestFolder, the code fails as the email is already in DestFolder.
How to detect if an email is already in the destination folder and move it to ONLY if it's not there already
Thank you in advance for your help
A simple method that may suffice.
On Error Resume Next
olItem.Move DestFolder
' Turn error bypass off once the purpose for it has been served
On Error GoTo 0
I have VBA code to download attachment immediately after mail comes into Outlook.
How do I invoke this code or is there another way to download attachments automatically?
Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim saveFolder As String
saveFolder = "D:\outlook\"
For Each objAtt In itm.Attachments
MsgBox objAtt
objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
MsgBox saveFolder & "\" & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
It looks like you are trying to create a rule which triggers a VBA script for saving attachments on the disk. If so, I'd recommend removing any MsgBox statements from the code. It may stop the code from running. Instead, you may use the Debug.Print statements in the code. See Where does VBA Debug.Print log to? for more information.
Instead of using rules you may consider handling the NewMailEx event of the Application class. The event fires once for every received item that is processed by Microsoft Outlook. The item can be one of several different item types, for example, MailItem, MeetingItem, or SharingItem. The EntryIDsCollection string contains the Entry ID that corresponds to that item.
Finally, you may find the Getting Started with VBA in Outlook 2010 article helpful.
So firstly, I'm very new to VBA and due to the number of emails I get that follow a certain template, I'm trying to automate the data collation to save myself from all the cutting and pasting that is currently required. I've looked at some previous questions but due to my very little knowledge, the answers aren't specific enough for me to understand.
Each one of these emails is from a particular email address and has a standard format as shown below:
"
dd/mm/yyyy hr.min.sec
xxx xxxxxxx xxxxxxxxxxxxxxxxx xxxx xxxxx "
I would like to export or copy this information to an excel 2003 worksheet so that each separate piece of information is in a new column of a single row, where each email is a new row.
I would like the macro to be able to search through my received emails in a particular folder (as I've already set up some rules in outlook relating to this email address), copy the information from each email matching the template and paste it into a single excel worksheet. Then each time I get a new email, the information will be added to the bottom of the table thats been created.
Hopefully that all makes sense, please let me know if you need anymore information.
Thanks in advance.
I did something exactly like this recently, except that I had it entered into an access database instead of an excel sheet, but the idea is the same. For some reason, I was having trouble getting it to run with rules, but I anyways found that I could control it better from a manually run macro. So use a rule to put everything into a folder, and make an AlreadyProcessed subfolder under that. Here is some code to start from:
Sub process()
Dim i As Integer, folder As Object, item As Object
With Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Folders("YourFolderName")
For Each item In .Items
processMail item
item.Move .Folders("AlreadyProcessed")
Next
End With
End Sub
Sub processMail(item As Outlook.MailItem)
Dim bitsOfInformation() As String
bitsOfInformation = Split(item.Body, " ")
'Use this information to make an Excel file
End Sub
Making Excel files from VBA are very easy - just read up on opening excel and making new documents from other Office program VBAs - you're looking for Excel.Application. You can even record a macro in Excel, filling the information manually, and basically copy the code into Outlook and replace the hard-coded information with variables. But if you're going to be running this on thousands of e-mails, be warned that recorded macros (that use selection objects) are inefficient.
Start with the following code:
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Set Items = GetItems(GetNS(GetOutlookApp), olFolderInbox)
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
End If
ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ProgramExit
End Sub
Function GetItems(olNS As Outlook.NameSpace, folder As OlDefaultFolders) As Outlook.Items
Set GetItems = olNS.GetDefaultFolder(folder).Items
End Function
Function GetNS(ByRef app As Outlook.Application) As Outlook.NameSpace
Set GetNS = app.GetNamespace("MAPI")
End Function
Function GetOutlookApp() As Outlook.Application
Set GetOutlookApp = Outlook.Application
End Function
This sets an event listener on your default Inbox. Whenever an email message is received, the code inside the If TypeName statement will be executed. Now it's simply a matter of what code you want to run.
You can check the sender using the .SenderName or .SenderEmailAddress properties to make sure it's the right sender.
If you provide more specific information, I can amend the code.
I have this piece of code, it works on one computer Outlook 2010 64bit but does not work on another machine running 32bit. I do not think this has anything to do with 32bit/64 but just want to mention it in-case.
I have tried everything, disable macro, turning Outlook inside out and cannot get this code to work.
Maybe a second eye will find something I am missing:
Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormat
dateFormat = Strings.Format(Now(), "mm_dd_yyyy_HH_MM_SS_AMPM")
saveFolder = "C:\Users\Jarvis\Desktop\Test"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & "My_Data_" & dateFormat & ".csv"
Set objAtt = Nothing
Next
End Sub
Thanks everyone!
Based on our chat, it seems the solution was to:
Copy EVERYTHING out of your VB macro project, into a text file. Save the textfile to your drive.
Close everything, reboot the computer.
Go to your C:\Documents and settings<yourusernamehere>\Application Data\Microsoft\Outlook folder.
Either delete the file VBAProject.otm, or (better and safer) rename to VBAProject.old or some similar name.
Open Outlook
Reopen the VB Macro editor
Paste in the code from your text file.
[Instructions courtesy of Arcane Code]