Company policy prevents .SaveAs when my code drafts an e-mail with an attachment.
I save the drafts into the Outlook folder, but the goal is to attach that .msg to another message.
Is there any way via VBA to create an e-mail and add the attachment by accessing the Outlook drafts folder?
.SaveAs and olSaveAsType fail due to company policy; unable to change registry to enable prompttosaveas (Error 287).
Unable to create from template due to variable file attachment within initial message.
You can copy the existing draft item in Outlook and continue setting it up for sending from the Drafts folder. The MailItem.Copy method creates another instance of an object, so the template saved in the Drafts folder will be remained as is.
Sub CopyItem()
Dim myNameSpace As Outlook.NameSpace
Dim myFolder As Outlook.Folder
Dim myItem As Outlook.MailItem
Dim myCopiedItem As Outlook.MailItem
Set myNameSpace = Application.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderDrafts)
Set myItem = myFolder.Items(1)
Set myCopiedItem = myItem.Copy
myCopiedItem.Send()
End Sub
Or you just can create a brand new email item in Outlook and then attach an existing item by using the Attachments.Add method which creates a new attachment in the Attachments collection. The source of the attachment. This can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment. So, you just need to specify an Outlook item instance you want to be attached.
Related
I would like to automatically print emails to PDF from outlook.
I haven't found a way to automate the print dialogue. There are a couple other threads dealing with this same issue in Outlook VBA, but no clear solution (I thought it would be simple!)
For example, I have a rule in outlook that automatically moves receipts to a specific folder. I'd like to automatically print these to PDF. I've tried to accomplish this by...
For Loop: Go through each unread item in the specified folder
Print: MailItem.Printout Method
Print Dialogue: Input path and filename and click OK. I haven't found any means of automating this process
Sub PrintReceipts()
'==============================================
'Declare variables, set namespace, define outlook folder (example names used below)
'==============================================
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim olFolder As Outlook.MAPIFolder
Dim msg As Outlook.MailItem
Dim Path As String
Dim Name As String
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set olFldr = objNS.GetDefaultFolder(olFolderInbox).Folders("subfolder 1").Folders("subfolder 2")
'==============================================
'For each unread message save to Path with Name and mark as Read (path is just an example)
'==============================================
For Each msg In olFldr.Items
If msg.UnRead Then
Path = "C:\Users\User\Desktop\"
Name = msg.Subject & ".pdf"
msg.PrintOut
'=================================================
'Here is where I get lost.
'Print Dialogue opens. I have tried SendKeys but it does not work
'=================================================
msg.UnRead = False
End If
Next
End Sub
Alternative: I am wondering if I can do the following...
Save for Word: MailItem.SaveAs, to save the item as an .MHT
Open Word: Somehow open Word and apply ActiveDocument.ExportAsFixedFormat to export as PDF
Close Word and go back to Outlook
I hope someone may have an idea!
First of all, iterating over all items in the folder is not really a good idea in Outlook. Instead, you need to use the Find/FindNext or Restrict methods of the Items class. These methods allow getting items that correspond to your search criteria only. Read more about these methods in the following articles:
How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
How To: Use Restrict method to retrieve Outlook mail items from a folder
To save the message body using the PDF file format there is no need to use the SaveAs method of the MailItem class. The WordEditor property of the Inspector class returns an instance of the Word Document class which represents the message body. You can call the ExportAsFixedFormat method of the Document class directly from Outlook avoiding any disk operations.
Dim objDoc As Object, objInspector As Object
Set objInspector = myItem.GetInspector
Set objDoc = objInspector.WordEditor
objDoc.ExportAsFixedFormat folderPath & fileName & ".pdf", 17
Set objInspector = Nothing
Set objDoc = Nothing
See Chapter 17: Working with Item Bodies for more information.
I am new in vb.net development and I must read informations (subject, body, ...) in Outlook email files that are in a disk directory (D:\mails\to-read\message1.msg, D:\mails\to-read\message2.msg, ...).
Is it possible ?
Can you please explain me ? With example ?
Thanks for your help.
If the Outlook object model (Outlook automation) is a possible option you can use the NameSpace.OpenSharedItem method which opens a shared item from a specified path or URL. This method is used to open iCalendar appointment (.ics) files, vCard (.vcf) files, and Outlook message (.msg) files. So, in the code you will get a MailItem object where you could get all the required properties.
Public Sub TestOpenSharedItem()
Dim oNamespace As Outlook.NameSpace
Dim oSharedItem As Outlook.MailItem
Dim oFolder As Outlook.Folder
' Get a reference to a NameSpace object.
Set oNamespace = Application.GetNamespace("MAPI")'Open the Signed Message (.msg) file containing the shared item.
Set oSharedItem = oNamespace.OpenSharedItem("C:\Temp\RegularMessage.msg")
MsgBox oSharedItem.Subject
oSharedItem.Close (olDiscard)
Set oSharedItem = Nothing
Set oSharedItem = Nothing
Set oFSO = Nothing
Set oNamespace = Nothing
End Sub
I am trying to grab target email received today from specific folder. My current VBA code is :
Sub ExportOutlookTableToExcel()
Dim oLookInspector As Inspector
Dim oLookMailitem As MailItem
Dim oLookWordDoc As Word.Document
Dim oLookWordTbl As Word.Table
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlWrkSheet As Excel.Worksheet
Dim Today As String
Today = Date
'Grab Email Item
Set oLookMailitem =Application.ActiveExplorer.CurrentFolder.Items("Apples Sales")
Set oLookInspector = oLookMailitem.GetInspector
Set oLookWordDoc = oLookInspector.WordEditor
However, my email is in specific folder called "Apples", if i move it to Inbox folder it works with CurrentFolder emthod. Is it any way to specify in which folder VBA should grab an email?
Assuming the folder is the subfolder on the Inbox folder, try
set folder = Application.Session.GetDefaultFolder(olFolderInbox).Folders("Folder Name")
if it is on the same level as the Inbox, use
set folder = Application.Session.GetDEfaultFolder(olFolderInbox).Parent.Folders("Folder Name")
You can use the NameSpace.PickFolder method which displays the Pick Folder dialog box. The Pick Folder dialog box is a modal dialog box which means that code execution will not continue until the user either selects a folder or cancels the dialog box. The method returns a Folder object that represents the folder that the user selects in the dialog box, or Nothing if the dialog box is canceled by the user.
So, each time your VBA macro runs you can choose the right folder at runtime. Otherwise, you will have to specify the hard-coded folder name.
The NameSpace.GetDefaultFolder method returns a Folder object that represents the default folder of the requested type for the current profile; for example, obtains the default ``InboxorCalendar` folder for the user who is currently logged on.
Then you can use the Folder.Folders property which returns the Folders collection that represents all the folders contained in the specified Folder. So, you may find a subfolder.
Set item = Application.GetNamespace("MAPI)".GetDefaultFolder(olFolderInbox).Folders("Apples").Items("Apples Sales")
I initially had this code running to save attachments from emails that come in. My code would loop through the entire folder and for any attachments that were there, it would save the attachment and remove it from the email. I had a rule in place to make the macro fire whenever the mail I wanted came in. However the attachment would never save down when the mail came. There were no errors, and debugging manually worked just fine. In addition, running the rule immediately afterwards by clicking the 'Run rules now' button would work just fine too. So after trying out a million different ways to save the attachments, I started getting really annoyed and set up a test to see what the hell outlook was doing. So here is the problem.
This is the outlook rule I have set up:
Apply this rule after the message arrives
from xxxxx
move it to the Macrotest folder
and run Project1.ThisOutlookSession.sayhi
I discovered though that what outlook does is fire the script Before it transfers the mail into the folder my macro looks in. Hence, it never finds the new file. Obviously then when its done the mail comes into the folder so when I run it manually it works just fine. So how can I fix the order that this rule comes in?
Public Sub sayhi(item As Outlook.MailItem)
Dim objNS As Outlook.NameSpace
Dim olfolder As Outlook.MAPIFolder
Dim msg As Outlook.MailItem
Set objNS = Application.GetNamespace("MAPI")
Set olfolder = objNS.GetDefaultFolder(olFolderInbox)
Set olsubfolder = olfolder.Folders("Macrotest")
Set oapp = CreateObject("Shell.Application")
For Each myitem In olsubfolder.Items
MsgBox "hellothere"
myitem.UnRead = False
Next
End Sub
The code processes the item you pass in (item As Outlook.MailItem)
Public Sub sayhi(item As Outlook.MailItem)
MsgBox "item Subject: " & item.Subject
End Sub
Process the attachments of "item", not all the mailitems in the folder.
I have a created folder in my outlook named "Reports". This folder contains emails with one attachment in each email. I would like to use ACCESS VBA to save the attachments from the "Reports" folder in Outlook to a local drive in my computer. here is the code I have so far, but gives me errors. Please help:
Sub GetAttachments()
Dim ns As NameSpace
Dim Inbox As Outlook.MAPIFolder
Dim folder As Outlook.MAPIFolder
Dim Item As Object
Dim Atmt As Attachment
Dim FileName As String
Dim i As Integer
Set ns = GetNamespace("MAPI")
Set Inbox = ns.Folders.Item("Reports") // I get an error in this line says an object could not be found
i = 0
If Inbox.Items.Count = 0 Then
MsgBox "There are no messages in the Inbox.", vbInformation, _
"Nothing Found"
Exit Sub
End If
For Each Item In Inbox.Items
For Each Atmt In Item.Attachments
FileName = "C:\Automation\" & Atmt.FileName
Atmt.SaveAsFile FileName // here is another error says method is not found
i = i + 1
Next Atmt
Next Item
Is your Reports folder within your Inbox folder? You may need to do something like this:
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set RptFolder = Inbox.Folders("Reports")
Your syntax for saving attachments looks correct (apart from your comments not being correct for VBA). You could print out the Filename that you are creating to see if it's a valid name. And I assume that you have created the Automation folder that you mention.
Update:
Try declaring your Atmt as an Outlook.Attachment. There is such a thing as an Access.Attachment which does not have a SaveAsFile method, and it's probably picking that one up first. If you include the library name, you should get the one you need.
Update 2:
To get to your Reports folder, one way is to get the Inbox folder as you are currently doing, then get its parent, then get the Reports folder under that.
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set Mailbox = Inbox.Parent
Set RptFolder = Mailbox.Folders("Reports")
Another way would be to scan the items under "ns" to find the one that starts with "Mailbox", then get the Reports folder under that. It seems a little more cumbersome than getting the parent of the inbox. That also seems cumbersome, but I couldn't find a way to get to the Mailbox folder directly.
Replace
For Each Item In Inbox.Items
For Each Atmt In Item.Attachments
FileName = "C:\Automation\" & Atmt.FileName
Atmt.SaveAsFile FileName // here is another error says method is not found
i = i + 1
Next Atmt
With.....
For Each Item In Inbox.Items
For Each Atmt In Item.Attachments
FileName = "C:\Automation\" & Atmt.FileName
Attachments.SaveAsFile FileName // here is another error says method is not found
i = i + 1
Next Atmt
Outlook does not have a problem with atmt in the reference however, MS Access does. This should fix your problem.
Davis Rogers
Replace
Dim Atmt As Attachment
with
Dim Atmt As Outlook.Attachment
It'll make Access find the correct Class for atmt.