HylaFAX The world's most advanced open source fax server

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

[hylafax-users] Mail Merge VBScript



I've done a little modification to the Word Mail merge
script that goes along with WHFC... 

The first change was to make it break out of the loop
if it can't talk to whfc.  Before, if you had a 500
record merge, you had to hit cancel 500 times, or
ctrl-alt-del.

Then I made it save your default printer (active
printer) name to a variable, then change it back after
the merge completed. Original behaviour was to just
make whfc fax the active printer, then leave it like
that.

I'll share it if anyone wants it:
(BTW, I don't know VB very well, so my changes may be
crap)




' ***************** macro FaxMailMerge begin
************************
Sub FaxMailMerge()
'
' Macro PrintAsFax
' this macro is an extension of the macro from Keith
Gray
' author: Detlev Reymann 1999, original macro: Keith
Gray
' detlev@xxxxxxxxxxxxxxxxx
' modded by bbutle01@gmail 2005
' purpose: create fax-documents and send them to
different adresses
' from MS-Word via whfc using the
mailmerge-possibilities of MS-Word
'
' the data source has to have a field which contains
the phrase fax
' (e.g. fax or fax-nbr or anything like this)
' from this field the number to send the fax to is
taken from
' the position of the field is not important!
'
' this is free software, you are allowed to copy and
modify it
' but you are not allowed to sell it in any way
' possibly we will extend this macro (e.g. automatic
installation)
'

' definitions
Dim whfc As Object
Dim OLE_Return As Long
Dim FaxNumber As String
Dim SpoolFile As String
Dim Title As String
Dim NbrOfFields As Integer
Dim FieldWithFaxNbr As Integer
Dim NbrOfFaxes As Integer
Dim i As Integer
Dim TelefaxNrField As Integer
Dim result As Integer

' the active document has to be a main mail-merge
document and it has to have
' an associated data-source document
' that is, what we test, if without succes we stop the
macro
If ActiveDocument.MailMerge.State <>
wdMainAndDataSource Then
    result = MsgBox("not a mail merge document or no
data source", vbInformation, "error")
    Exit Sub
End If

' Now we see how many faxes to print
' the number of faxes is the number of the last record
ActiveDocument.MailMerge.DataSource.ActiveRecord =
wdLastRecord
NbrOfFaxes =
ActiveDocument.MailMerge.DataSource.ActiveRecord

' make the first record the active record
ActiveDocument.MailMerge.DataSource.ActiveRecord =
wdFirstRecord

' now we look which of the field names contains the
phrase fax.
' this is the field we take the fax-number from
NbrOfFields =
ActiveDocument.MailMerge.DataSource.DataFields.Count


If Tasks.Exists("WHFC") = False Then
    ' if it is not running we start whfc
    Dim result2 As Integer
    result2 = -1
    ' in the following line you eventually have to
modify the programm-location !!!
    result2 = Shell("C:\program
files\whfc\whfc\Whfc.exe", 6)
    ' if we are not able to start whfc message and
exit the macro
    If result2 < 0 Then
        result2 = MsgBox("can not start whfc",
vbInformation, "attention")
        Exit Sub
    End If
End If


' we save the field-number in FieldWithFaxNbr
For FieldWithFaxNbr = 1 To NbrOfFields
    If InStr(1,
ActiveDocument.MailMerge.DataSource.DataFields(FieldWithFaxNbr).Name,
"fax") > 0 Then
        TelefaxNrField = FieldWithFaxNbr
        Exit For
    End If
Next FieldWithFaxNbr
' if we don't find a field wich contains the phrase
fax
' we stop the macro
If i > NbrOfFields Then
    result = MsgBox("can not find a field for the
fax-numbers", vbInformation, "error")
    Exit Sub
End If

' we make a OLE-connection to whfc
Set whfc = CreateObject("WHFC.OleSrv")

' we need a postscript printer.
' I had some problems as I tried to use the printer,
which is
' connected to the WHFC-Port
' So I installed a separate printer which is connected
to a file
' You have to modify the following line to meet your
system settings !!!
OldActivePrinter = ActivePrinter
ActivePrinter = "WHFC Fax"

' now we print a fax for all data-records which
contain a faxnumber
For i = 1 To NbrOfFaxes
    ' for each fax we create a temporary file
    ' you have to adapt the path to your
system-settings !!!
    SpoolFile = "C:\windows\temp\fax" & i & ".ps"
    Title = "WHFC OLE MailMergeMacro"
     
    ' to be able to use the field contents of the
active record
    ' within the active mailmerge-document
    ' we have to do:
    ' show the fields not the field-codes
    ActiveWindow.View.ShowFieldCodes = False
    ' show the values of the active record
    ActiveWindow.View.MailMergeDataView = True
          
    ' we take the faxnumber from the field we found
before
    FaxNumber =
ActiveDocument.MailMerge.DataSource.DataFields(FieldWithFaxNbr)
    
    ' we only send a fax if there is a faxnumber
    If FaxNumber > "" Then
        ' we print the whole document to the temporary
file
        Application.PrintOut FileName:="",
Range:=wdPrintAllDocument,
Item:=wdPrintDocumentContent, Copies:=1, Pages:="",
PageType:=wdPrintAllPages, Collate:=True,
Background:=True, PrintToFile:=True,
OutputFileName:=SpoolFile, Append:=False
    
        ' the file is send to whfc via OLE
        OLE_Return = whfc.SendFax(SpoolFile,
FaxNumber, True)
        ' if it does not work - error message
        If OLE_Return <= 0 Then
            result = MsgBox("error connecting to
WHFC", 16, Titel)
            Exit Sub
        End If
    End If
    ' get next record
    ActiveDocument.MailMerge.DataSource.ActiveRecord =
wdNextRecord
Next i

' stop OLE-connection
Set whfc = Nothing

' activation of the standard printer
' please adapt to your system settings !!!

ActivePrinter = OldActivePrinter
End Sub
' ***************** macro MailMergeFax end
************************





		
____________________________________________________
Start your day with Yahoo! - make it your home page 
http://www.yahoo.com/r/hs 
 

____________________ HylaFAX(tm) Users Mailing List _______________________
  To subscribe/unsubscribe, click http://lists.hylafax.org/cgi-bin/lsg2.cgi
 On UNIX: mail -s unsubscribe hylafax-users-request@xxxxxxxxxxx < /dev/null
  *To learn about commercial HylaFAX(tm) support, mail sales@xxxxxxxxx*




Project hosted by iFAX Solutions