Log in

View Full Version : [SOLVED:] OpenSharedItem Opening a custom form msg is always blank



WallyZ
11-26-2015, 06:26 PM
I trying to open a custom form message saved to disk but all I get a blank message. The message header appears correct so I assume I have got the correct message.

When opening the same custom form msg from Explorer it all works fine.

Here is the code:


Private Sub PrintPreviewCustomFormMsgFromSavedDiskFile_Click()
Dim oOLapp As Outlook.Application
Dim oMapi As Outlook.NameSpace
Dim oInspector As Outlook.Inspector
Dim oCB As Office.CommandBars
Dim oItem As Outlook.MailItem
Dim MyPath As String

MyPath = "C:\temp\Custom Form Example 1.msg" ' Form opens/displays correctly from Explorer

Set oOLapp = GetObject(, "Outlook.Application")
Set oMapi = oOLapp.GetNamespace("MAPI")

Set oItem = oMapi.OpenSharedItem(MyPath) ' Either this or the next ...
Set oItem = oOLapp.Session.OpenSharedItem(MyPath) ' but not both of course


oItem.Display ' Displays blank message ?

Set oInspector = oOLapp.ActiveInspector
Set oCB = oInspector.CommandBars


oCB.ExecuteMso ("FilePrintPreview") ' A blank message results in a blank preview !
Set oCB = Nothing
Set oInspector = Nothing
End Sub

Any ideas how this should be done correctly?

WallyZ
11-26-2015, 10:23 PM
In addition stepping through the code and comparing a MailItem object referenced in a pst store to one opened from a saved file location (using OpenSharedItem) reveals the ModifiedFormPages and UserProperties are all missing!

Hence why it doesn't display the form.

WallyZ
11-27-2015, 12:55 AM
Further if I move the opened MailItem to a Folder ("Drafts") and then re-open it from the folder then it works!


Set oOLapp = GetObject(, "Outlook.Application")
Set oFolder = oOLapp.GetNamespace("MAPI").Folders(oMapi.DefaultStore.DisplayName).Folders("Drafts")

oItem.Move oFolder
oItem.Close olDiscard

Set oItem = oFolder.Items(1) ' Assumes Drafts was empty prior to moving item
oItem.Display ' Now displays correctly


The issue I know have is I don't know when the user has finished viewing the MailItem and so have no idea when to clear up the (Temporary) Folder.

Is there a way to automatically delete a MailItem on closing?

WallyZ
11-27-2015, 01:22 AM
The trick was to move the MailItem into a temporary Folder (Deleted Items) and use the MailItem object returned by the move.
The clearing up issue becomes irrelevant.

complete code follows:


Private Sub PrintPreviewCustomFormMsgFromSavedDiskFile_Click()
Dim oOLapp As Outlook.Application
Dim oMapi As Outlook.NameSpace
Dim oInspector As Outlook.Inspector
Dim oCB As Office.CommandBars
Dim oItem As Outlook.MailItem
Dim oFolder As Outlook.Folder
Dim oItems As Outlook.Items
Dim MyPath As String

MyPath = "C:\temp\Custom Form Example 1.msg"

Set oOLapp = GetObject(, "Outlook.Application")
Set oMapi = oOLapp.GetNamespace("MAPI")
Set oFolder = oOLapp.GetNamespace("MAPI").Folders(oMapi.DefaultStore.DisplayName).Folders("Deleted Items")

Set oItem = oOLapp.Session.OpenSharedItem(MyPath)
Set oItem = oItem.Move(oFolder)
oItem.Display

Set oInspector = oOLapp.ActiveInspector
Set oCB = oInspector.CommandBars


oCB.ExecuteMso ("FilePrintPreview")

Set oCB = Nothing
Set oInspector = Nothing
End Sub