MSMQ supports sending and receiving serialized objects that support IPersistStream and IPersistStorage. Many persistent objects, such as all Microsoft Office documents, can be sent as the body of an MSMQ message.
When sending an object as the body of a message, MSMQ seamlessly invokes the object's IPersist interface. In this way, when the message is sent, the persistent state of the object is serialized into the message body using the object's supplied IPersist interface. At the receiving end, an implementation of the object's interface must be installed to use the object when it is removed from the queue.
The example below sends a Microsoft Word 8.0 document to a destination queue. This code could be modified to send any persistent object.
qinfo.PathName = ".\Objecttest"
qinfo.Label = "My Object Test Queue"
On Error Resume Next 'Ignore if queue already exists.
qinfo.Create
On Error Goto 0 'Reset error handler.
Set qSend = qinfo.Open(MQ_SEND_ACCESS, MQ_DENY_NONE)
Set msWordDoc = msWordApp.Documents.Open("FileName:=pathname\filename", ConfirmedConversions:=False, ReadOnly:=True)
mSend.Label = "Testing Object Message"
mSend.Body = msWordDoc
mSend.Send qSend
qSend.Close
Set qReceive = qinfo.Open(MQ_RECEIVE_ACCESS, MQ_DENY_NONE)
Set mReceive = qReceive.Receive
Set objReceive = mReceive.Body
The following example creates a Microsoft Word document object, sends the object as an MSMQ message to a destination queue, retrieves the message from the queue, and then creates a new object using the body of the retrieved message. The new object is testing to see if it is a Word document object.
Option Explicit
Dim msWordApp As New Word.Application
Dim msWordDoc As Word.Document
Dim objReceive As Object
Dim qinfo As New MSMQQueueInfo
Dim qSend As MSMQQueue
Dim qReceive As MSMQQueue
Dim mSend As New MSMQMessage
Dim mReceive As MSMQMessage
Private Sub Form_Click()
'*******************************************************************
' Create a destination queue and open it with SEND access.
'*******************************************************************
qinfo.PathName = ".\ObjectTest"
qinfo.Label = "My Object Test Queue"
On Error Resume Next 'Ignore if queue already exists.
qinfo.Create
On Error GoTo 0
Set qSend = qinfo.Open(MQ_SEND_ACCESS, MQ_DENY_NONE)
'*******************************************************************
' Create document object.
'*******************************************************************
Set msWordDoc = msWordApp.Documents.Open("FileName:=pathname\filename", ConfirmedConversions:=False, ReadOnly:=True)
'*******************************************************************
' Create a message and send it to the destination queue. Set the
' body of the message to the document object.
'*******************************************************************
mSend.Label = "Testing Object Message"
mSend.Body = msWordDoc
mSend.Send qSend
qSend.Close
MsgBox "Document object sent as body of message."
'*******************************************************************
' Open the destination queue with Receive access, and retrieve the
' message.
'*******************************************************************
Set qReceive = qinfo.Open(MQ_RECEIVE_ACCESS, MQ_DENY_NONE)
Set mReceive = qReceive.Receive
qReceive.Close
'*******************************************************************
' Retrieve the message from the queue and create new document
' object. Test the object to verify it is a Microsoft Word document.
'*******************************************************************
Set objReceive = mReceive.Body
If TypeOf objReceive Is Word.Document Then
MsgBox "Received message body is a Microsoft Word document."
Else
MsgBox "Received message body is unknown object."
End If
End Sub