Sending a Persistent Object

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.

    To send a Microsoft Word 8.0 document
  1. Make sure that Microsoft Word 8.0 is installed on your computer.
  2. Add a reference to Microsoft Word 8.0 in your Microsoft Visual Basic project. This example was tested with the following library referenced.
  3. 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                'Reset error handler.
    Set qSend = qinfo.Open(MQ_SEND_ACCESS, MQ_DENY_NONE)
     
  4. Create the Microsoft Word document object.
    Set msWordDoc = msWordApp.Documents.Open("FileName:=pathname\filename", ConfirmedConversions:=False, ReadOnly:=True)
     
  5. Create and send a message. Set the body of the message to the document object.
    mSend.Label = "Testing Object Message"
    mSend.Body = msWordDoc
    mSend.Send qSend
    qSend.Close
     
  6. Open the queue with receive access and retrieve the message.
    Set qReceive = qinfo.Open(MQ_RECEIVE_ACCESS, MQ_DENY_NONE)
    Set mReceive = qReceive.Receive
     
  7. Create a new object using the body of the retrieved message. The example below then tests the new object to verify that it is a Microsoft Word document object.
    Set objReceive = mReceive.Body
     

Example Code

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