Control already created Outlook message from excel VBA - vba

longtime reader, first time messenger (not my first time making a bad joke though),
I would like to know if it is possible to gain control of an Outlook email message that has already been created. At work we have to download new work orders from a secure website, thanks mostly to this site, I have been able to set up a macro that logs in, finds the new work orders, and clicks the button to open the work order. Once this button is clicked, a new IE window is opened with a pdf file, and the "Send page by email" command is used to create a new outlook message. I have the outlook 12 reference (using Office 2007), and am able to take control of an existing outlook session to create a new email using:
Dim SendOrder As Outlook.Application
Set SendOrder = GetObject(, "Outlook.Application")
But I cannot figure out how to make it control the email message that was opened by IE. I tried using GetObject(, "Outlook.Application.MailItem), and a few other failed ideas, but 3 ideas were all I had, so I'm hoping someone on here can help me out with this, otherwise I'll probably have to save the file in IE and create a new email message, which seems like adding an extra step.

You're on the right path, I think. Something like this works with Outlook mailItems opened from Outlook. I have not tested it on mailItems opened from IE, though.
Sub GetAMailItem()
'## Requires reference to MS Outlook object library ##
Dim oApp As Outlook.Application
Dim mItem As MailItem
Set oApp = GetObject(, "Outlook.Application")
If TypeName(oApp.ActiveWindow) = "Inspector" Then
Set mItem = oApp.ActiveWindow.CurrentItem
End If
Set oApp = Nothing
End Sub
Found the guts of that code here, just made a modification or two to give you a structured example that might suit your needs.

Related

outlook mailitem in explorer mode is old

I have an intermittent updating problem with an outlook mailitem in the explorer mode on a public folder of an exchange server. The subject (or perhaps any other part as well) of a mailitem does not seem to update itself at times.
Specifically, the subject of the mailitem shown in the explorer mode is dissimilar to that shown in the inspector mode. In other words, the explorer mode is old, therefore needs updating or synchronization.
Hitting F9 (send/receive) to update the public folder does not seem to help.
I would like to know if this anomaly can be detected (and perhaps perform manual updates/synchronization) using an outlook macro.
Any ideas are welcome.
Some properties, factors unknown, update on saving only.
The subject of the mail in the explorer view should change when you manually close the item, if you agree to save changes. Should be the same if you manually saved regularly.
If you want more than a simple save:
Option Explicit
Sub MarkInUseSaveCurrentItem()
Dim currItem As Object
Set currItem = ActiveInspector.currentItem
If InStr(LCase(currItem.subject), LCase("Barok is working on this")) = 0 Then
currItem.subject = "Barok is working on this. To avoid conflicts do not open. " & currItem.subject
End If
currItem.Save
End Sub

VBA: not all Outlook Emails show up

I wrote a small piece of code, that checks an additional inbox (besides my main-adress in Outlook) for E-Mails.
The problem shows up, when using the code on a pc, where only the additional inbox is added to outlook (as the main inbox). Obviously, the code can't retrieve all Emails, only the older ones. That's awkward, because the path to the subfolders and even the emails seem to be found, but not the newer ones. I can see them in Outlook with no problem.
Does anyone have an idea, why this is happening? As I told, the same code works with no problem on a PC with an Outlook-Installation, with another main-inbox and the inbox that needs to be checked as additional one.
That's the code I use to access Outlook and the Emails:
Dim objFolder As Outlook.Folder
Dim objOL As Outlook.Application
Set objOL = CreateObject("Outlook.Application")
Set objFolder = objOL.GetNamespace("MAPI").Folders.Item("test#test.de").Folders.Item("Posteingang").Folders.Item("Subfolder-Name").Folders.Item("Subfolder-Name-2")
With objFolder.Items(1)
...
I had this same issue: in my case the emails were in microsoft exchange but were not downloaded to the local outlook.
If you can refresh the outlook it should resolve the problem
Why are you always retrieving the first item in the Items collection? Would you not want to loop through the items?
set objItems = objFolder.Items
objItems.Sort "[ReceivedTime]"
'now objItems is sorted
I have found that changing the cache mode setting in Outlook helps with this problem of emails being in Microsoft Exchange not being accessed thru Excel VBA, especially Step (3) below.
https://support.microsoft.com/en-us/office/turn-on-cached-exchange-mode-7885af08-9a60-4ec3-850a-e221c1ed0c1c
Click File > Account Settings > Account Settings.
Click the Exchange or Microsoft 365, and then click Change.
Under Offline Settings, check Use Cached Exchange Mode.
(If you're a Microsoft 365 subscriber with semi-annual updates, under Offline Settings, check Use Cached Exchange Mode to download email to an Outlook data file.

Outlook ItemAdd event on IMAP folder fires only when folder is selected

I'm running in a little problem with Outlook VBA programming, and would like to know if there's a solution, or if this is just another "known issue".
Context:
I have configured an Outlook e-mail account to access my web email provider through IMAP. In Outlook, I can properly see my web email folders. My provider's spam filter moves spam messages into the Spam folder.
I would like to automatically move messages that get put into the Spam folder into another folder, in my local pst file.
I have it working 99% (through the code provided below for reference).
Issue:
I can see that there are messages in the Spam folder (there is a bold unread message count beside the folder name), but the ItemAdd even will only fire when I click on the folder. At that point, I see the contents of the spam folder, and then see all of the new spam being moved to my local folder.
Is there another trigger source beside ItemAdd I could use for running my code without having to click on the folder? Is there an event that gets triggered when the unread count for a folder changes?
Technical details:
Windows 8 OS
Using Outlook 2002 (Yes, I know...)
I'm an experienced C/C++ developer, but minimal experience in VBA, and none with Outlook.
VBA code:
Public WithEvents myItems As Outlook.Items
Public Sub Application_Startup()
Dim myNameSpace As Outlook.NameSpace
Const mailboxName As String = "Mail.com"
Const subfolderName As String = "Spam"
' Reference the items in the MAPI spam folder
' Because myOlItems is declared "WithEvents" the ItemAdd event will fire below.
Set myNameSpace = Application.GetNamespace("MAPI")
On Error GoTo noSpamFolder
Set myItems = myNameSpace.Folders(mailboxName).Folders(subfolderName).Items
On Error GoTo 0
Exit Sub
noSpamFolder:
MsgBox "Unable to find folder <" & mailboxName & "/" & subfolderName & ">"
End Sub
Private Sub myItems_ItemAdd(ByVal Item As Object)
Dim suspectFolder As Outlook.MAPIFolder
' Check to make sure it is an Outlook mail message, otherwise
' subsequent code will probably fail depending on what type
' of item it is.
If TypeName(Item) = "MailItem" Then
' Move message to the 'suspect' folder
On Error GoTo noSuspectFolder
Set suspectFolder = Outlook.Session.GetDefaultFolder(olFolderInbox).Folders("suspect")
On Error GoTo 0
Item.Move suspectFolder
End If
Exit Sub
noSuspectFolder:
MsgBox "Unable to find folder <suspect> as a sub-folder of default inbox folder"
End Sub
I Was struggling with a similar issue to move mail-items after they were sent and used your code to perform this task (thx!). There were several issues which still had to be resolved.
First of all, the items were moved, but immediately after they were placed into the trash folder. This seems to be an IMAP issue (Gmail) and may be resolved by changing the Internet E-mail Settings of the mailbox account from "Move deleted items to the following folder on the server" to "Mark items for deletion but do not move them automatically".
The second challenge was, like yours, trigger the code to do its work. In the account configuration the save sent emails option is disabled (since this is automatically performed by the Gmail server). I needed to sync the Sent items (MAPI) folder with the Send items (IMAP) folder. I achieved this by configuring the "send/receive" groups for this email account (in the group All account) and selecting the Sent items folder.
Now this folder is synced without the necessity to open the folder for syncing. I hope that this will also resolve your issue.
Peter
That makes sense - the IMAP provider in Outlook syncs the folder only when it is selected or accesed through the Outlook Object Model.
I don't think there is much you can do short of polling the folder every once in a while (and releasing the MAPIFolder object in between the hits)

How to Set Outlook in Work Offline Mode by VBA(Macro) Running From MS Word VBA UserForm

I am trying to run a VBA code which it's supposed to let the user attach a file into a Mail Merge function.
In order to do this I need to put Outlook in Offline mode to keep the mails in outbox before attaching any files to mails. Now I would like to know if there is a method to put Outlook 2007 in Offline mode from a Word Macro?
First, add a reference to the Outlook Object Library. If Outlook will already be open:
Outlook.ActiveExplorer().CommandBars.FindControl(, 5613).Execute
if Outlook is closed:
Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")
olApp.GetNamespace("MAPI").Folders.GetFirst.GetExplorer.CommandBars.FindControl(, 5613).Execute

Directory picker for Visual Basic macro in MS Outlook 2007

I wrote a Visual Basic macro for archiving attachments for Outlook 2007, but did not find a totally satisfactory way for showing a directory picker from the Outlook macro. Now, I don't know much about either Windows APIs or VB(A) programming, but the "standard" Windows file dialog I see most often in Microsoft applications would seem like an obvious choice, but it does not seem to be easily available from Outlook's macros.
Ideally, the directory picker should at least allow to manually paste a file path/URI as a starting point for navigation, since I sometimes already have an Explorer window open for the same directory.
What are the best choices for directory pickers in Outlook macros?
Two things I already tried and did not find totally satisfactory are (the code is simplified and w/o error handling and probably also runs in older Outlook versions):
1) Using Shell.Application which does not allow me to actually paste a starting point via the clipboard or do other operations like renaming folders:
Set objShell = CreateObject("Shell.Application")
sMsg = "Select a Folder"
cBits = 1
xRoot = 17
Set objBFF = objShell.BrowseForFolder(0, sMsg, cBits, xRoot)
path = objBFF.self.Path
2) Using the Office.FileDialog from Microsoft Word 12.0 Object Library (via tools/references) and then using Word's file dialog, which somehow takes forever on my Vista system to appear and does not always actually bring Word to the foreground. Instead, sometimes Outlook is blocked and the file dialog is left lingering somewhere in the background:
Dim objWord As Word.Application
Dim dlg As Office.FileDialog
Set objWord = GetObject(, "Word.Application")
If objWord Is Nothing Then
Set objWord = CreateObject("Word.Application")
End If
objWord.Activate
Set dlg = objWord.FileDialog(msoFileDialogFolderPicker)
path = dlg.SelectedItems(1)
Any other ideas?
Your best bet will probably be to use the Windows32 API for this. See this MSDN article for sample VBA code on how to interact with the API.
The article outlines a few different techniques, but I'd suggest searching the article for "COMDLG32.dll" and following the steps outlined in that section.