The RPC server is unavailable (outlook in vb.net) - vb.net

I am developing little application. It's purpose is on button click generate some email and open outlook "new email" window with this template. I am using Outlook interop 14. On application start I create New Outlook.Application() instance. This creates outlook.exe process.
My method for generating and open email:
Private Sub GenerateEmail(...)
...
'this line throws RPC exception when no outlook.exe process exists
Dim OutlookEmail As Outlook.MailItem = CType(OutlApp.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)
...
OutlookEmail.Display()
...
End Sub
When I run an application, outlook.exe process starts. I click generate email, outlook opens window BUT when I close this "new email" window it also terminates outlook.exe process. And this is problem becasue second time I want to generate an email I am getting an error "The RPC server is unavailable". The question is how to close the window but not terminate outlook process. Btw I am getting the same error also when I am try to second time create a new instance of Outlook.Application (after terminating a previous one).

Outlook quits (which can take some time) when its last visible window closes. To prevent that from happening, retrieve a MAPIFolder object (e.g. using Application.Session.GetDefaultFolderFolder(olFolderInbox)) and store the Explorer object returned by calling MAPIFolder.GetExplorer() in a global/class variable. The Explorer object does not have to be made visible.

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

Slow opening of any email after set value for an object on Application_ItemLoad

Basically I am using below code to run macro after I manually open a specific email.
The code and macro works ,but I noticed there is a slowness while opening of any email on outlook.
After many tests, I find out this line is the cause of issue:
Set MyItem = Item
And this the full code:
Option Explicit
Option Compare Text
Public WithEvents MyItem As Outlook.MailItem
Private Sub Application_ItemLoad(ByVal Item As Object)
If Item.Class = olMail Then
Set MyItem = Item 'This line cause slow opening of any email
End If
End Sub
Private Sub myItem_Open(Cancel As Boolean)
If MyItem.Subject = "Test Email" Then
'Code
End If
End Sub
Kindly How to fix this issue?
Basically I am using below code to run macro after I manually open a specific email.
You may consider using other event handlers in Outlook - for selecting in the Explorer window you may try to handle the SelectionChange event of the Explorer class which is fired when the user selects a different or additional Microsoft Outlook item programmatically or by interacting with the user interface. For opening in the separate window 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. Also you may handle the Inspector.Activate event which is fired when an inspector becomes the active window, either as a result of user action or through program code.
If there is a specific reason to use the ItemLoad event handler in the code you may continue using the late-binding technology and keep the item defined as object.
Also don't forget to release corresponding objects in the Unload event of the Item-level events.

In vb.net when calling a method from an instance of Outlook.Application, a pop up halts code execution

We have a vb.net windows application that creates an instance of Outlook.Application to open an email .MSG file, and save it as a .HTML by calling the Outlook.Application SaveAs() method.
However, sometimes the call to .SaveAs() causes a popup to appear, for example when saving a .MSG that is digitally signed, there is a confirmation popup.
This popup causes the code execution to halt until the user interacts and clicks YES or NO on the pop up. Since our application runs on a server and requires no user interaction this causes the application to halt periodically until a technician logs into the server and clicks 'yes' on the popup.
Is there a way to avoid this pop up, or have 'YES' automatically get selected?
I've tried investigating the documentation for Outlook.Application but of course the documentation is very lacking on MSDN.
Here is some example vb.net code:
outlookapp = CreateObject("Outlook.Application")
Dim olItem As Object = outlookapp.CreateItemFromTemplate("C:\msg\message.msg")
olItem.SaveAs("C:\html\convertedmessage.html", 5)
If message.msg is digitally signed, when calling SaveAs() to save it as an HTML file, you get a pop up that the MSG is digitally signed, this halts the code until you click yes or no.
Note: Outlook objects do not support the DisplayAlerts flag of Word objects.
Firstly, Outlook is not designed to be used in a service or a server side application with no user present.
In your particular case, you can do the following:
1 Use Extended MAPI (C++ or Delphi only) to open the MSG file (OpenIMsgOnIStg etc.), then construct the HTML file explicitly in your code.
2 Parse the MSG file (its format is documented) or use one of several commercial components (such as the one from Aspose) to read the MSG files.
3 Use Redemption (I am its author) and its RDOSession.GetMessageFromMsgFile / RDOMail.SaveAs(..., olHTML) methods to open the MSG file and perform the conversion.
dim Session As RDOSession = CreateObject("Redemption.RDOSession")
dim Msg As RDOMail = Session.GetMessageFromMsgFile("C:\msg\message.msg")
Msg.SaveAs("C:\html\convertedmessage.html", 5)

How to check in VBA that "no document is open"

I'm asking this question, because when the document is opened in safe mode, my macro crashes, pointing to this line:
strInput = ActiveDocument.Content
So, to prevent this error message:
This command is not available because no document is open. Error code 4248
I want to check whether there are opened documents or not. And if not, then to set some timer and loop until the user changes the mode of the document.
Do you mean the protected mode?
In that case you can handle the ProtectedViewWindowActivate event of the Application class which is fired when any protected view window is activated.
See Working with Document Protected View Events in Word for more information.

Outlook MailItem cache issue when saving in 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.