Visual Basic Code Example: Sending Msg to a Destination Queue


Applies To: Windows 10, Windows 7, Windows 8, Windows 8.1, Windows Server 2008, Windows Server 2008 R2, Windows Server 2012, Windows Server 2012 R2, Windows Server Technical Preview, Windows Vista

This example provides a private Sub procedure that sends a message to a single private or public destination queue on the local computer or a remote computer.


Remote queues can be opened to send messages while offline. To do this, your application must either obtain and cache the format name of a remote queue while online or have the information needed to construct a direct format name for the queue. This example creates a direct format name from the computer name and queue name provided by the caller.

For information on sending messages, see Sending Messages.

To send a message to a destination queue

  1. Declare the objects needed to send the message. This procedure declares the following objects.

  2. Generate a direct format name using the computer name and queue name provided by the caller.

  3. Set the MSMQQueueInfo.FormatName property to the direct format name created.

    Alternatively, except in the case of a remote private queue, you can create the path name of the queue from the computer name and queue name supplied by the caller, and set the PathName property of the MSMQQueueInfo object using the following lines of code.

    ' Create the path name of the queue.  
    Dim strPathName As String  
    strPathName = strComputerName & strQueueName  
    ' Set the PathName property of the MSMQQueueInfo object to  
    ' the path name created.  
    qinfo.PathName = strPathName;  

When the PathName property is set, Message Queuing must obtain the format name of the queue before opening it. The public format name of any public queue must be retrieved from the directory service, and the private format name of a local private queue can be obtained from information stored on the local computer.

  1. Call MSMQQueueInfo.Open to create an open instance of the queue.

  2. Set message properties. This procedure sets the MSMQMessage.Label property of the message to "Test Message".

  3. Call MSMQMessage.Send to send a copy of the message to the destination queue referenced by the format name and close the queue.

Code Example

The following example can be run on all versions of Message Queuing.

Sub SendMessage( _  
                strComputerName As String, _  
                strQueueName As String _  
  Dim strFormatName As String  
  Dim qinfo As New MSMQQueueInfo  
  Dim qDest As MSMQQueue  
  Dim msg As New MSMQMessage  
  ' Create a direct format name.  
  strFormatName = "DIRECT=OS:" & strComputerName & "\" & strQueueName  
  ' Set the FormatName property of the MSMQQueueInfo object.  
  On Error GoTo ErrorHandler  
  qinfo.FormatName = strFormatName  
  ' Open the queue.  
  Set qDest = qinfo.Open(MQ_SEND_ACCESS, MQ_DENY_NONE)  
  ' Set message properties.  
  msg.Label = "Test Message"  
  ' Send the message and close the queue.  
  msg.Send DestinationQueue:=qDest  
  Exit Sub  
  MsgBox "Error " + Hex(Err.Number) + " was returned." _  
         + Chr(13) + Err.Description  
End Sub