HylaFAX The world's most advanced open source fax server

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

Mailmerge and print makros for whfc and MS-Word



Hello,
I wrote two macros for whfc and MS-Word which might be of interest for 
somebody.
The first one simple faxes the active Document via whfc and the second one 
makes it possible to use the mailmerge-function of MS-Word to send faxes with 
different contents to different faxnumbers.
Feel free to give the macros to everbody as long as you don't charge any fee 
for that.
' ***************** macro PrintAsFax begin ************************
Sub PrintAsFax()
'
' Macro PrintAsFax
' author: Detlev Reymann 1999
' detlev@reymann-online.de
' purpose: print the whole active document
' from MS-Word via whfc
'
' this is free software, you are allowed to copy and modify it
' but you are not allowed to sell it in any way
'
' the lines following those ending with exclamation marks possibly have
' to be modified to meet your system settings !!!

' my hylafax-server is not always online
' so I don't load whfc via autostart but
' I test wether whfc is running otherwise it is started
If Tasks.Exists("WHFC") = False Then
    ' if it is not running we start whfc
    Dim result As Integer
    result = -1
    ' in the following line you eventually have to modify the programm-location !!!
    result = Shell("C:\program files\whfc\Whfc.exe", 6)
    ' if we are not able to start whfc message and exit the macro
    If result < 0 Then
        result = MsgBox("can not start whfc", vbInformation, "attention")
        Exit Sub
    End If
End If

' Now we make the hylafax-printer the active printer.
' You have to adapt this to your system settings.
' If you are not shure about the name of your whfc-printer
' start MS-Word, activate the printer menu and have a look at the list
' which contains the printer names
ActivePrinter = "FaxPrinter"

' default is to print the whole document
Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
    wdPrintDocumentContent, Copies:=1, Pages:="", 
PageType:=wdPrintAllPages, _
    Collate:=True, Background:=True, PrintToFile:=False

' At the end of this macro we activate the standard printer again
' you have to adapt htis to your own settings again !!!
ActivePrinter = "standard printer"
End Sub
' ***************** macro PrintAsFax end ************************


' ***************** 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@reymann-online.de
' 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

' 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 !!!
ActivePrinter = "Apple Color LW 12/600 PS"

' 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:=wdPrintCurrentPage, _
			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)
		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 = "standard printer"
End Sub
' ***************** macro MailMergeFax end ************************

Greetings

Detlev
**************************************************************
Diese Mail ist mit PGP signiert. Wenn Sie nicht mit PGP arbeiten,
ignorieren Sie die entsprechenden Textabschnitte einfach.
This mail is signed with PGP. If you do not work with PGP
simply ignore the curious signs in this mail.
**************************************************************

Prof. Dr. Detlev Reymann
FH Wiesbaden
Studienstandort Geisenheim
Fachbereich Gartenbau und Landespflege
von-Lade-Strasse 1
Tel. 06722-502732
Fax: 06722-502380
EMail: D.Reymann@geisenheim.fbl.fh-wiesbaden.de
Meine Webseite: http://www.mnd.fh-wiesbaden.de/~dreymann




Project hosted by iFAX Solutions