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")
Related
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.
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.
The title is pretty self-explanatory.
I want the macro to open a specific mailbox and then maximize that newly created explorer window so that the mailbox that was just opened is full-screen.
Here is the code I have so far:
Public Sub openMasterfiles()
' Define Variables
Dim olNS As Outlook.NameSpace
Dim myFolder As Outlook.MAPIFolder
' Set objects
Set olNS = GetNamespace("MAPI")
Set myFolder = olNS.Folders("Masterfiles").Folders("Inbox")
' Display the second mailbox
myFolder.Display
' ###Atttempting to set the new mailbox to the active explorer here
Set Application.ActiveExplorer.CurrentFolder = myFolder
' Attempting to maximize the newly opened mailbox here. This code _
only maximizes the exisiting explorer. Not the new one.
Application.ActiveWindow.WindowState = olMaximized
End Sub
All this manages to accomplish is to open the new mailbox ("Masterfiles") and maximize the prior Outlook window, not the newly opened one.
Thanks in advance!
First of all, to get the Inbox folder you need to use the NameSpace.GetDefaultFolder method which returns a Folder object that represents the default folder of the requested type for the current profile.
If you need to get the Inbox folder from an additional store you can use the Store.GetDefaultFolder method which returns a Folder object that represents the default folder in the store and that is of the type specified by the FolderType argument.
But the key thing here is that you need to use the Activate method of the Explorer class which activates an explorer window by bringing it to the foreground and setting keyboard focus.
' Display the second mailbox
myFolder.Display
Set Application.Explorers.Item(2).CurrentFolder = myFolder
Application.Explorers.Item(2).Activate
' Attempting to maximize the newly opened mailbox here. This code _
only maximizes the exisiting explorer. Not the new one.
Application.ActiveWindow.WindowState = olMaximized
You may also check whether it is a second explorer window checking the Explorers.Count property which returns an integer indicating the count of objects in the specified collection.
You can use Explorers.Add, which takes MAPIFolder as an argument:
set expl = Application.Explorers.Add(myFolder, olFolderDisplayNormal)
expl.Display
expl.WindowState = olMaximized
I receive emails that contain a link. That link does not work since I am not on that company's network. I can change part of the link for external use to get it to work.
For example the email has this link:
https://ipdms.web.companyname.com/ipdms/itemlocation
I change it to:
https://companyVPN.companyname.com/ipdms/itemlocation
I was able to create a script but I need to open the email, run the macro, and then hit save on the email.
Sub Change2VPN()
Application.ActiveInspector.CurrentItem.body = _
Replace(Application.ActiveInspector.CurrentItem.body, "ipdms.web", "companyVPN")
End Sub
I searched but have not been able to get anything to work. Is there a way I can either accomplish this on all items in a folder and save the email where it is or at least do it from the reading pane?
I can add the macro button to the ribbon.
I cannot run scripts as a rule on incoming emails due to corporate policies.
Basically you need to get the currently selected folder where a ribbon button was clicked and iterate over all items in the folder to get the job done:
Sub Change2VPN()
Dim olFolder As Outlook.Folder
Dim Item As Object
Dim explorer as Outlook.Explorer
Set explorer = Application.ActiveExplorer()
Set olFolder = explorer.CurrentFolder
For Each Item In olFolder.Items
If TypeOf Item Is Outlook.MailItem Then
Dim oMail As Outlook.MailItem: Set oMail = Item
oMail.HTMLBody = Replace(oMail.HTMLBody, "ipdms.web", "companyVPN")
oMail.Save()
End If
Next
End Sub
The aim of this project is to:
Load template from network personal drive (currently working)
Open Dialog Window, using Word (as that loads quicker for me), set to a different network folder (function achieved - just)
Choose and attach user chosen file (prefered option for multi-file selection but that was to complex and thus beyond the time I can afford to dedicate to the project)
Using previous filepath, build and apply the subject line based on the file name. (currently working)
The macro is called by a button in the Quick Access or Main Tool Bars.
The problems:
when the "Browse" window appears, it does so behind outlook/hidden UNLESS the macro code has been viewed in the editor at some time since Outlook was opened; every fix either show the entire Word program or seems to have no effect.
when you choose a file in the "Browse" and then click "OK", a second "Browse" window opens where you have to choose a file again and this time click "Open"; this second file is then the one that is attached to the email.<--FIXED
System Details (even though not all will be of importance)
Windows 7 64bit
Office Professional 2010
The Code
Sub New_Orders_Email()
Dim NewMail As Outlook.MailItem
Dim otherObject As Word.Application
Dim fd As Office.FileDialog
Dim fileaddress As String
Dim filename As String
On Error GoTo Final
'Set template to use
Set NewMail = Application.CreateItemFromTemplate("P:\Office Templates\Orders SCI - 1617.oft")
'Set to use Word for Attach File Dialog
Set otherObject = New Word.Application
otherObject.Visible = False
Set fd = otherObject.Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = False
.InitialFileName = "R:\Science\Technician Documents\Budgets & Orders\2016 - 2017\Orders\"
.Show
End With
fd.Show
fileaddress = fd.SelectedItems(1)
'Aquire File Name in correct form for Subject Line
Dim MidStart As Long
MidStart = InStrRev(fileaddress, "\") + 16
Dim MidEnd As Long
MidEnd = InStrRev(fileaddress, ".")
filename = Mid(fileaddress, MidStart, MidEnd - MidStart)
'Load template, attach single file and apply correct Subject
NewMail.Display
NewMail.Attachments.Add (fileaddress)
NewMail.Subject = "Order No: " + filename
otherObject.Quit
Set otherObject = Nothing
Final:
End Sub
Your file Dialog is probably not shown, because of the line "otherObject.Visible = False"
Number (2) might be solved if you delete one of your "fd.Show". Looks like you call it twice...