HOWTO: Sending Persistent Objects Using MSMQ in Visual Basic

ID: Q175872

The information in this article applies to:

SUMMARY

This article describes how to send and receive persistent objects such as Microsoft Office documents with an Microsoft Message Queue Server (MSMQ) message.

MORE INFORMATION

The MSMQMessage.body property can be any intrinsic Variant, including string, date, currency, number as well as byte array and persistent object.

MSMQ's ActiveX implementation supports sending and receiving serialized objects which support IPersistStream and lPersistStorage. There are many persistent objects, such as all Microsoft Office documents, that can be sent as MSMQ messages.

The MSMQ support for "persistent objects" means that the MSMQ ActiveX components seamlessly invokes the object's IPersist interface when sending that object as the message body. That is, the persistent state of the object will be serialized into the message body, using the object's supplied IPersist interface. An implementation of the object's interface is assumed to be installed on the receiving end. For example, you can specify an Excel spreadsheet as the message body, but you will need Excel on the receiving end to do anything useful with it.

The following Visual Basic 5.0 example code opens a Word document using the method supplied by Word Object library (add a reference to "Microsoft Word8 Object Library" in your project), assigns the document to message body, and sends a message.

   Private Sub Form_Load()
      Dim myQueueInfo As New MSMQQueueInfo
      Dim myMessage As New MSMQMessage
      Dim myNewMessage As New MSMQMessage
      Dim myQueue As MSMQQueue
      Dim myWordApp As New Word.Application
      Dim myWordDoc As Word.Document
      Dim myObj As Object

      ' pass the pathname of the Word document, make sure the file exists
      Set myWordDoc = myWordApp.Documents.Open("wordfile.Doc", False, True)

      myQueueInfo.PathName = ".\PersistQ"
      myQueueInfo.Label = "Test Queue"
      myQueueInfo.Create
     ' Open for send
      Set myQueue = myQueueInfo.Open(MQ_SEND_ACCESS, MQ_DENY_NONE)

      myMessage.Body = myWordDoc
      myMessage.Label = "Message"
      myMessage.Send myQueue
      MsgBox "Word Doc is sent"  ' can verify in Explorer

      myQueue.Close
     ' Open for Receive
      Set myQueue = myQueueInfo.Open(MQ_RECEIVE_ACCESS, MQ_DENY_NONE)
      Set myNewMessage = myQueue.Receive(wantBody:=True, _
                                      ReceiveTimeout:=1000)
      Set myObj = myNewMessage.Body
      If TypeOf myObj Is Word.Document Then
          MsgBox "Word Doc!"
      Else
          MsgBox "?"
      End If
      myQueue.Close
   End Sub

REFERENCES

MSMQ SDK Help; search for "Body property"

Keywords          : MQProg 
Version           : WINNT:1.0
Platform          : winnt
Issue type        : kbhowto

Last Reviewed: November 1, 1997