HOWTO: Sending Persistent Objects Using MSMQ in Visual Basic

Last reviewed: October 31, 1997
Article ID: Q175872
The information in this article applies to:
  • Microsoft Message Queue Server version 1.0

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


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 31, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.