How To Send a Fax Message with an Attachment Using CDO (285993)



The information in this article applies to:

  • Collaboration Data Objects (CDO) 1.21
  • Collaboration Data Objects (CDO) 1.2
  • Microsoft Visual Basic Learning Edition for Windows 6.0
  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Exchange Server 5.5

This article was previously published under Q285993

SUMMARY

This article demonstrates how to programmatically send a fax with an attachment using Collaboration Data Objects (CDO) 1.2x.

MORE INFORMATION

NOTE: You must have fax capabilities through a fax gateway or on Microsoft Windows 95 or Windows 98. In general, if you can send a fax using Microsoft Outlook, you should be able to fax using this method.

The attached file must also be of a type that has a registered OLE association with an application that knows how to print the document; this is because that application will be used to render the document. For instance, a .doc file might be rendered by Microsoft Word or WordPad, a .xls file requires Excel, Notepad handles a .txt file, and so on.

To run this sample, follow these steps:
  1. Create a new Visual Basic project.
  2. On the Project menu, click References, and then select Microsoft CDO 1.21 Library.
  3. Add a button to the form and paste the following code below into the Click subroutine:
    Private Sub Command1_Click()
    
    Dim objSession 
    Dim objMessage 
    Dim objRecipient 
    
    On Error Resume Next 
    
    'Create The Session Object. 
    Set objSession = CreateObject("Mapi.Session")
    
    'Logon to Mailbox. 
    'TODO: Change the line below to represent the profile that you are using.
    objSession.Logon "<Outlook Profile Name>" 
    
    'Create the Message Object. 
    Set objMessage = objSession.Outbox.Messages.Add 
    
    If err.number <> 0 Then 
    	err.Clear 
    	MsgBox "Failed To LogOn Succesfully" 
             Exit Sub
    Else 
    	MsgBox objSession.CurrentUser & " logged on succesfully." 	
    End If 
    
    'Set up Message. 
    objMessage.Subject = "Test" 
    objMessage.Text = "This is a test" 
    
    'Create the Recipient Object. 
    set objRecipient = objMessage.Recipients.Add 
    
    'Set up Recipient. 
    'Note: For sample fax syntax, see the Knowledge Base article listed
    'later in this article. Because other third-party vendors may have a 
    'different syntax, please verify the syntax with your vendor before 
    'using this code.
    
    objRecipient.Name = "[FAX:faxnumber]" 
    objRecipient.Type = 1 
    objRecipient.Resolve 
    
    'TODO: Make sure to have a file created in the location indicated in the 
    'line below.
    objMessage.Attachments.Add "test.txt",0,  1, "C:\test.txt" 
    
    If err.number <> 0 Then 
    	MsgBox "There were errors" 
    Else 
    	MsgBox "Fax Sent!" 
    End If 
    
    
    'Send Message.          
    objMessage.Send 
    
    'Log off Session. 
    objSession.Logoff 
    
    'Destroy All Objects 
    Set objRecipient = Nothing 
    Set objMessage = Nothing 
    Set objSession = Nothing 
    End Sub
    					
  4. Run the project and click the button.

REFERENCES

For additional information on the fax syntax, click the article number below to view the article in the Microsoft Knowledge Base:

181222 OL98: (CW) How to Type a Fax Number on the To Line of a Mail Msg


Modification Type:MinorLast Reviewed:7/13/2004
Keywords:kbhowto KB285993