Log in

View Full Version : Change Active Printer to Fax Printer



mnarewec
04-02-2009, 12:18 AM
Team
I am able to send fax using my code below if the fax printer is the default printer. However I am not able to fax if it is not an active printer (run time error : 2147023741 - Operation Fail). I tried to setting the active printer to the fax printer, which it did when I debug it and I also see it change on the Printers and Faxes folder, however it still came up with the same runtime error.

It seems WORD is not recognising the change, but it does when I restart MS Word and re-run the macro.

Please assist

Private Function Letter_Printed_On_MSFax(deviceInfo, faxNumber, faxRecipient, faxCompany, faxSubject, FileName) As Boolean
' This routine attempts the print the merged letter to the Microsoft fax. If the print is successful, then it
' returns true. If the print fails, then (depending on what kind of error) it either returns false, or
' it stops the macro.
On Error GoTo FaxErrorHandler
Dim objFaxDocument As New FAXCOMEXLib.FaxDocument
Dim collFaxRecipients As FaxRecipients
Dim JobId As Variant
Dim strMsg As String
Dim sTifPath As String
Dim success As Boolean
Dim faxPort As String
Dim faxPrinterName As String
Dim index As Integer

success = True
faxPrinterName = Fax_Printer
sTifPath = TempMSFaxDirectory + TempMSFaxFile

'Error handling
On Error GoTo FaxErrorHandler

'Print active document to temp directory to be faxed
ActiveDocument.PrintOut _
Background:=False, _
Append:=False, _
Range:=wdPrintAllDocument, _
OutputFileName:=sTifPath, _
Item:=wdPrintDocumentContent, _
Copies:=1, _
PageType:=wdPrintAllPages, _
PrintToFile:=True
'Set the Active Printer to fax printer
faxPort = WordBasic.[GetPrivateProfileString$]("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Devices\", "WinFax", "")
index = InStr(1, faxPort, ",")
faxPort = Mid$(faxPort, index + 1, Len(faxPort))

If faxPort = "" Then
MsgBox ("The Fax Printer Driver for device " + faxPrinterName + " does not appear to be installed." + vbCrLf + _
"You must re-install WinFax." + vbCrLf + vbCrLf + _
"The mail-merge macro will stop.")
Documents.Close (wdDoNotSaveChanges) ' close all documents
End ' terminate the macro
End If

faxPrinterName = faxPrinterName + " on " + faxPort
'WordBasic.FilePrintSetup printer:=faxPrinterName, DoNotSetAsSysDefault:=1
Dim strPrinter As String
strPrinter = ActivePrinter
ActivePrinter = faxPrinterName


'Set Fax Document
Set objFaxDocument = New FAXCOMEXLib.FaxDocument

'Set the fax body
objFaxDocument.Body = sTifPath

'Name the document
objFaxDocument.DocumentName = FileName
objFaxDocument.Subject = faxSubject

'Get the recipients collection
Set collFaxRecipients = objFaxDocument.Recipients

'Add the recipients
With collFaxRecipients
.Add DialingRules & faxNumber, faxCompany
End With

'Load the default sender
objFaxDocument.Sender.LoadDefaultSender

'Group the broadcast receipts
objFaxDocument.GroupBroadcastReceipts = True

'Connect to the fax server, submit the document, and get back the
'job ID array. "" indicates the local server.

JobId = objFaxDocument.Submit("MAILMERGE")

'Reset the Active Printer
ActivePrinter = strPrinter

'Remove the recipients from the collection. If you don't take this step,
'and run this code again without closing the program, the recipients
'collection will retain the recipients and keep adding more recipients.
'The count and item numbering will change as you remove the items, so
'just remove item (1) Count times

Dim i As Integer
Dim lCount As Long
lCount = collFaxRecipients.Count
For i = 1 To lCount
collFaxRecipients.Remove (1)
Next


Letter_Printed_On_MSFax = success

Exit Function
FaxErrorHandler:
'If the WinFax Printer entry is not found Stop the Macro and inform the user to
're-install WinFax
MsgBox ("An error has occurred while faxing." + vbCrLf + vbCrLf + _
"The mail-merge macro will stop.")
MsgBox (Err.Number & " - " & Err.Description)
Documents.Close (wdDoNotSaveChanges) ' close all documents
End ' terminate the macro
End Function

Nelviticus
04-02-2009, 01:43 AM
Nobody will be able to run your code because they won't have a reference to FAXCOMEXLib.

What line does it fail on?

mnarewec
04-02-2009, 02:55 PM
Line:
JobId = objFaxDocument.Submit("MAILMERGE")

Nelviticus
04-03-2009, 01:25 AM
This is just a guess, but I suspect that some of the properties of objFaxDocument are invalid. Check the values that are assigned by the lines:
.Add DialingRules & faxNumber, faxCompany
and
objFaxDocument.Sender.LoadDefaultSender
are valid.

Unfortunately I can't run the code so all I can do is guess.

Regards