Outlook MailItem cache issue when saving in vb.net - vb.net

I have a small WinForms program that allows my users to create email blasts for our clients. The app has two options: one is an HTML editor to design the email (works great) and the second is to import .msg or .oft template.
Once the email is complete it is moved to a shared outlook mailing folder for a nightly macro send job.
Pretty simple stuff!
The problem: Once the template is open in the application outlook seems cache that version. If the user decided to get out make a change in the template Outlook doesn't pick up the update.
Note: If the users clicks on the "Preview" button they received the correct UPDATED version in their inbox. But when they submit the MailItem it picks up the old version.
Dim newItem as Outlook.MailItem = gobjOutlook.CreateItemFromTemplate(fileEmailTemplate.FileName)
The send command works fine newItem.Send()
But when I move it to the shared folder it gets the original version from somewhere.
Dim addFldr As Outlook.MAPIFolder
addFldr = StoreFLDR.Folders.Add(gobjNamespace.CurrentUser.Name & ": " & DateTime.Now.ToString())
newItem.Save()
newItem.Move(addFldr )
I have tried forcing the GC and SaveAs to another location and reload the template, no luck.

I'd suggest starting from releasing underlying COM objects instantly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object.
You may find the How To: Create a new Outlook message based on a template article helpful. Anyway, it would be great to see your full source code related to Outlook.

Related

Automatically Closing Outlook Windows via Script/Macro

I'm hoping someone on here can point me in the right direction with some expertise. I have a situation with a client's outlook where we need to close any open draft windows after a new message/draft is opened. Ideally after the 4th new window has opened.
Any "new message" window that opens after this we need the script to close 1st window that opened. Either killing the process, or something similar.
Recently have been looking into Outlook macros, but am unsure if they will help in this instance. (Maybe they are?). Being more familiar with Powershell, figured we could start there.
Looking to get help writing a powershell script, macro, etc to do this on the backend.
The Outlook object model provides all the required events, methods and properties for that. So, VBA macros is the right choice if you don't need to distribute the solution on multiple machines. Otherwise, you need to consider developing a COM add-in instead (for example, a VSTO based one should work for you). See Walkthrough: Create your first VSTO Add-in for Outlook for more information.
You can handle the NewInspector event which is fired whenever a new inspector window is opened, either as a result of user action or through program code. The event occurs after the new Inspector object is created but before the inspector window appears.
You can also check the number of opened inspector windows in Outlook by using the Inspectors.Count property which returns a long indicating the count of objects in the specified collection.
Finally, the Inspector.Close method closes the Inspector and optionally saves changes to the displayed Outlook item. For example, a VBA sample which closes the active inspector instance:
Sub CloseItem()
Dim myinspector As Outlook.Inspector
Dim myItem As Outlook.MailItem
Set myinspector = Application.ActiveInspector
Set myItem = myinspector.CurrentItem
myItem.Close olSave
End Sub

Changing Font Type of Messages in Outlook when double-clicking a .vbs file [duplicate]

This question already has an answer here:
How can I change the default font of Outlook 2016?
(1 answer)
Closed 3 months ago.
I have no experience nor any idea about VB scripts but I was tasked to create a .vbs file wherein when I double-click that file, it will automatically change the default font in Outlook 2016 to Montserrat. I've been picking up the pieces of whatever resource I could find online but it seems that the scripts I'm making do not change anything particular nor any message that it really ran successfully. I'm basically double-clicking the .vbs scripts I made and nothing happens. Below is what I had so far but I was told that that the .DefaultFont feature is not supported.
Sub ChangeFont()
Dim objOLApp
Dim NewTask
Set objOLApp = CreateObject("Outlook.Application")
Set NewTask = objOLApp.CreateItem(0)
with Newtask
.DefaultFont = "Montserrat"
End With
On Error GoTo 0
Set objOLApp = Nothing
Set NewTask = Nothing
END Sub
Basically what happens is when I run the script then open Outlook 2016, all the font configs will be set to Montserrat style like in the image below:
Is there any starting point anyone can recommend for this? Been stuck in this task for weeks. Thank you in advance.
I've already tried several code snippets I outsourced on the documentation and available resources online. Some kind-of makes sense to me but I'm not really sure how to match the pieces. I was expecting to create a .vbs file wherein when I double-click that file or open the file, the fonts of the running Outlook instance messages would change from the default font to Montserrat. I'm not really sure if this is possible but any help is highly appreciated.
First, in the code you are creating a new MailItem instance, not a task item like it was named.
Second, Outlook items like MailItem or TaskItem don't provide the DefaultFont property.
Third, I've already described possible ways of changing the font-related properties in Outlook. Check out your post - Automatically Change Default Font of Outlook 2016 using Visual Basic Script. In brief, you can use the HTML markup to specify a custom font or use the Word object model to deal with item bodies.
Fourth, you can find some Outlook settings in the Windows registry. So, to locate where exactly Outlook keeps Outlook settings I'd suggest using the Process Monitor utility, so you could do the required changes in the UI and track where they are saved.
You may also find similar questions posted over the internet, see Set Font Style and Size of email message called from VB.net.

Catching "move to explorer" event from Outlook

Recently I have written small tool (vb.net WinForms) for our company that creates structure and archives all Emails that are in x, y Folder in Outlook. This tool then renames and converts these .msg files to .eml files.
Now it would be perfect, if these Emails would automatically convert themself when user places it directly in the destination in Windows-Explorer folder per Drag and Drop. For that I assume I would have to run script/programm whenever Event X (moving Email from Outlook to explorer) occurs.
Has anyone an idea if catching that event would be possible? Or maybe any better ideas? I thought of some scripts that run on server and check for new files (once daily for example), but the first solution is more appearing to me.
You can catch the Items.ItemAdd event on the folder to track and convert new items.
The Outlook object model doesn't provide any events when items are saved to a folder by dragging and dropping items. You can use a file system watcher to track folder changes, see VBA monitor folder for new files for more information.

Error reading Outlook HTML Body

I'm getting error 438 : object doesn't support this property or methodwhen I am trying to run the macro on a new machine:
Set HTMLdoc = New MSHTML.HTMLDocument
With HTMLdoc
.body.innerHTML = OutlookMail.HTMLBody **'******** Error Line**
Set tables = .getElementsByTagName("table")
End With
Yes it is working fine in my system. But when I tried to install this in new system, it creates this error.
No changes in the code, I am running the same excel macro file on some outllok mails.
Even the outlook mails which I used for testing, i am using the same files in the new machine.
Also, the Tool>>Reference libraries, I have checked all the Items like MS HTML object library, MS Outlook 16.0 Object library and other default checkboxes are maintained same in the system.
The code abruptly breaks when it reaches this line :
. Body. Innerhtml = Outlookmail. HTMLBody
.body.innerHTML = OutlookMail.HTMLBody
The inner HTML markup of the body doesn't equal to the HTML markup of the Outlook's message body. It includes other outer html markup that should be removed before.
Anyway, VBA macros are not designed for distributing them on multiple machines. That is exactly COM add-ins were introduced for. In that case you will be able to create an installer for your solution to get it deployed automatically for all users. Moreover, you will be able to check and install (if needed) the required prerequisites at the installation time. See Walkthrough: Create your first VSTO Add-in for Outlook to get started quickly.

How to Send All Emails From Outlook Outbox in VBA

I'm using Excel VBA to stage emails in Outlook and it is working well.
Dim template As Outlook.MailItem, tomerge As Outlook.MailItem
' Create E-mail
tomerge.Close olSave
The e-mails can then be manually moved to the Drafts folder and sent using this Sub.
'Loop through items in Drafts folder
objDrafts.Item(i).Send
However, many users have a bunch of extra drafts in their Drafts folder that they don't want sent.
If I replace "olFolderDrafts" with "olFolderOutbox" and try to send from their Outbox. The first message sends and then I get a "Run-time error" "Outlook has already begun transmitting this message".
Is there some way to send all from the Outbox or even better is there someway to stage and send from a newly created folder?
You need to create a folder for your unsent items and process them separately. As a rule the Outbox folder contains already submitted items. So, that's not a right place for your items.
The Outlook Object Model provides the Add function of the Folders class. You can get an instance of the Folders class using the Folders property of the Folder class in Outlook. You can read more about that in the How To: Create a new folder in Outlook article.
This answer was inspired by Nagarajan's comment above, but there are quite a few changes necessary from the answer in Send/Receive in Outlook Via Code. The main problem is using olSave doesn't put the messages in a "ready to send" state in Outlook so starting a sync using syc.Start from the answer above does nothing.
Instead, we found the following process to be straight forward:
Put Outlook in offline mode using "Send/Recieve" -> "Work Offline"
Use Excel VBA to stage the e-mails and instead of saving just .Send each email. As Outlook is offline they will be staged and ready to be sent but not actually sent.
The e-mails should now be staged in Outlook's Outbox folder and can be reviewed just be sure to press the "send" button after reviewing/editing messages otherwise the e-mail you modified will be removed from the queue.
When the messages are ready to be sent put Outlook back in online mode and they will be sent automatically.