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
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 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
I am using the Outlook Contact Form from my application to allow the user to create a new contact. When the user saves the contact or closes the form, the instance of outlook closes as well. How can I keep outlook from closing, I am not done with the object and it takes a few seconds to open another instance of outlook?
Dim outlookApp as new Outlook.Application
Dim newContact as New Outlook.ContactItem
newContact = outlookApp.CreateItem(Outlook.OlItemType.olContactItem)
newContact.Display(True)
Outlook exits when its last window (explorer or inspector) closes even if there are outstanding references to its objects.
Try to keep a reference to an Outlook explorer (it does not have to be visible) if there isn't one open. Off the top of my head:
dim explorer as Outlook.Explorer
...
explorer = outlookApp.ActiveExplorer
If (explorer Is Nothing) Then
session = outlookApp.GetNamespace("MAPI")
session.Logon
folder = session.GetDefaultFolder(olFolderInbox)
explorer = folder.GetExplorer
End If