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:
- Create a new Visual Basic project.
- On the Project menu, click References, and then select Microsoft CDO 1.21 Library.
- 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
- Run the project and click the button.