Sub MergeFax()
'
' MergeFax Macro
' Macro created 08/11/98 by Keith Gray
' Modified for full-blown Mailmerge 10/30/00 by Michael DeWitt
'
' This version will create a mailmerge with output for each record to be merged and produce a PS file suitable for Hylafax. The script
' was tested with word97 sr-1.  Consider the first page of your merge document to be the cover page. The number of pages might be able to
'be determined in VBA, but I just type it into the body of the document (since all of the faxes are the same length.
' How to use this script:
'1. Create your mailmerge document
'2. Run the mailmerge script setting your main document and data source.
'3. Modify the line setting datafields("fax") to be the correct name of the fax number field in your merge document.
'4. Make sure that the merge document is the "active" document (if you have other documents open in Word).
'5. call your hylafax printer (created for WHFC) "hylafax" or modify the name in the script below. (it needn't be this printer, but why create another PS Printer?

'It is important to note that the WHFC does not support killtime as of yet and so if you are sending a lot of faxes, you may find that some of them are are killed before
'they even get a chance to be sent.  In a dual modem hylafax server, sending a 6 page fax, 5-6 faxes were aborted before they had a chance to be
'sent.

Dim whfc As Object
Dim OLE_Return As Long
Dim faxnum As String
Dim SpoolFile As String

Dim Template As String
Dim Tempfile As String
Dim mergedfile As String
Dim Title As String
Dim WhfcPrinter As String
Dim Default_Active_Printer As String
Dim Box_Return As Integer
' MsgBox ActiveDocument.Name
ActiveDocument.MailMerge.ViewMailMergeFieldCodes = True
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdLastRecord
Numb_Faxes = ActiveDocument.MailMerge.DataSource.ActiveRecord
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdFirstRecord
Template = ActiveDocument.Name

For I = 1 To Numb_Faxes

SpoolFile = Environ("Temp") & "\fax" & I & ".ps"

Title = "WHFC Mail Merge to Fax Macro( Version 1.1 )"

faxnum = ActiveDocument.MailMerge.DataSource.DataFields("fax") '<===Change
"fax" to be the correct name of the fax number field


With ActiveDocument.MailMerge
    .DataSource.FirstRecord = I
    .DataSource.LastRecord = I
    .Destination = wdSendTofile
    .SuppressBlankLines = True
    .Execute
End With


' MsgBox ActiveDocument.Name
Tempfile = ActiveDocument.Name
WhfcPrinter = "Hylafax"
Default_Active_Printer = ActivePrinter
'MsgBox ActivePrinter
ActivePrinter = WhfcPrinter$

'Note: background must be false in order to work - this is do to some kind
of bug in VBA

Application.PrintOut FileName:="", Range:=wdPrintAllDocument, _
    Item:=wdPrintDocumentContent, Copies:=1, Pages:="", _
    PageType:=wdPrintAllPages, Collate:=True, Background:=False, _
    PrintToFile:=True, OutputFileName:=SpoolFile, Append:=False

  Set whfc = CreateObject("WHFC.OleSrv")
  OLE_Return = whfc.SendFax(SpoolFile, faxnum, True)
  ' MsgBox OLE_Return 'if you activate this you will see the fax job number
from hylafax.
  If OLE_Return <= 0 Then
    Box_Return = MsgBox("Error sending file", 16, Title)
  Else
    'Box_Return = MsgBox(OLE_Return, 0, Title)
  End If
  Set whfc = Nothing
  
Documents(Tempfile).Close (False)
Documents(Template).Activate
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord
Next I

ActivePrinter = Default_Active_Printer$
End Sub



