View Full Version : Printing to several files using a Function


Aenathras
06-13-2007, 12:54 AM
I don't know if anyone can advise me on this one.

I have a Public Function in a Module that I would like to call from several places from several forms but the file number I am printing to varies so I can create the files in a single data parse.

At the moment I have the following:

Public Function PrintToFile(FileNumber As Integer, strCol1 As Variant) As Integer

If FileNumber = 1 Then
Several 'Print #1' lines
Print #1, strCol
Several 'Print #1' lines
End If

If FileNumber = 2 Then
Several 'Print #1' lines
Print #1, strCol
Several 'Print #1' lines
End If

If FileNumber = 3 Then
Several 'Print #1' lines
Print #1, strCol
Several 'Print #1' lines
End If

PrintToFile = 1

End Function


While what I would like - to simplify my code - is to have something like the pseudo-code:

Public Function PrintToFile(FileNumber As Integer, strCol1 As Variant) As Integer

Several 'Print #1' lines
Print FileNumber, strCol
Several 'Print #1' lines

PrintToFile = 1

End Function

However I haven't been able to find a way to parameterise the Print command so that number of the file #1, #2, #3 etc. get passed into the function.

Has anyone come accross this one before?

Aenathras

boblarson
06-13-2007, 01:01 AM
Have you tried

Print #FileNumber, strCol

I seem to remember seeing this before.

Aenathras
06-13-2007, 01:22 AM
Thank-you for that one - it did just the job.

I am used to other languages where I would have used a & e.g. &FileNumber

Hopefully your prompt and accurate response will help someone else too


:)
Aenathras

boblarson
06-13-2007, 06:30 AM
Glad to hear it worked.