Sample VB Script to Send a Fax Programmatically

Below is the VB-script for Windows Script Host (cscript.exe/wscript.exe). This script can be easily customised for VB6 or VBA script (Microsoft Office). For more advanced scripting using VB6 and VBA, consider to add a COM reference to Microsoft Fax Service Extended COM Type Library (Windows XP and above), or FaxCom Type Library (Windows 2000).

Sub CheckError(strDetails)
   Dim strErr
   If Err.Number <> 0 then
     strErr = strDetails & " : Exception " & Err.Description & " err.Number=0x" & Hex(Err.Number)
     WScript.Echo strErr
     WScript.Quit(Err.Number)
   End If
End Sub

For Windows XP and above

On Error Resume Next
Set FaxServer = WScript.CreateObject("FAXCOMEX.FaxServer")
CheckError("WScript.CreateObject(FAXCOMEX.FaxServer)")
WScript.Echo "FaxServer created"
'    Connect to the fax server. Specify computer name if the server is remote. See How to connect to a remote Fax Service for details.
FaxServer.Connect ""
CheckError("FaxServer.Connect")

Set FaxDoc = WScript.CreateObject("FAXCOMEX.FaxDocument")
CheckError("WScript.CreateObject(FAXCOMEX.FaxDocument)")

'    Set file name of any printable document.
FaxDoc.Body = "test.rtf"
CheckError("FaxDoc.Body")
FaxDoc.DocumentName = "My First Fax"
'    Add recipient's fax number. If this string contains a canonical fax number
'    (starting with plus + followed by a country code, an area code in round brackets
'    surrounded by spaces and a fax number), the Fax Service will translate
'    that number into dialable format in accordance with your current location.
'    Otherwise, make sure the international prefix or long distance prefix is specified when needed,
'    as the fax number will be passed on to a fax driver (Fax Service Provider) unchanged.
'    For example, sending a fax from San Francisco to Sydney's fax number 123456, the canonical address
'    +61 (2) 123456 will be translated into dialable address 011612123456.
'    If you are using T37FSP in conjunction with Internet Fax Service, specify absolute address
'    612123456 (without leading plus, to avoid translation into dialable format),
'    as most Internet Fax Services expect the number in the absolute format.
'    To take advantage of Windows Fax Service outbound routing available on Windows Server
'    fax address must be specified in canonical format.

FaxDoc.Recipients.Add ("612123456")

'    Optionally, set the sender properties.
'    T37FSP uses only FaxDoc.Sender.Email in Windows Server 2003 for delivery status notifications.
'    In Windows Server 2008, T37FSP derives email address from FaxDoc.Sender.Name via facsBridge.xml file.

FaxDoc.Sender.Email = "bob@xyz.com"
FaxDoc.Sender.Name = "Bob"
FaxDoc.Sender.FaxNumber = "7777777"

'    Optionally, Use FaxDoc.CoverPage and FaxDoc.CoverPageType to specify a cover page
'    FaxDoc.CoverPage = generic
'    FaxDoc.CoverPageType = 2

'    Optionally, you can control banner in outbound faxes
FaxServer.Folders.OutgoingQueue.Branding = True '    True to set banner on, False to set banner off
FaxServer.Folders.OutgoingQueue.Save '      Make the change persistent
'    Optionally, use FaxServer.Folders.OutgoingQueue.Retries and
'    FaxServer.Folders.OutgoingQueue.RetryDelay to control retries

'    Submit the document to the connected fax server and get back the job ID.
JobID = FaxDoc.ConnectedSubmit(FaxServer)
CheckError("FaxDoc.ConnectedSubmit")
WScript.Echo "FaxDoc.ConnectedSubmit success"

For Windows 2000

On Error Resume Next
Set FaxServer = WScript.CreateObject("FaxServer.FaxServer")
CheckError("WScript.CreateObject(FaxServer.FaxServer)")
WScript.Echo "FaxServer created"

'    Connect to the fax server.
FaxServer.Connect ""
CheckError("FaxServer.Connect")

Set FaxDoc = FaxServer.CreateDocument("test.rtf")
CheckError("FaxServer.CreateDocument")

'     Add recipient's fax number. See above comments on the fax number format.
FaxDoc.FaxNumber = "612123456"

'    Submit the document to the connected fax server
FaxDoc.Send
CheckError("FaxDoc.Send")
WScript.Echo "FaxDoc.Send success"