Discussion Locked

Visual Basic and Printing

0 helpful
8 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I have a manual with about 50 chapters. Each chapter is a seperate file that is located in a seperate directory.

How can I build code that can print these files without opening them as an active document?

I would like to create a button that will print all the files in sequence.

Thank you!

8 replies

by kees.valkenswaard

You make it very complicated, 50 files in 50 directories.
Suppose they are all in the same directory, then you could use something like this:

Sub printall(Mydir)
ChangeFileOpenDirectory MyDir
myName = Dir(*.doc) ?this will give the first filename ending with .doc, if any
If myName = "" Then
Exit Sub
End If
Do
? open file with name in myName and print it + close it again
? look for this coding yourself
myName = Dir ?This will result in the next filename, if any.
loop whilemyName <> ""
end sub

I did not add all the declarations and error handling.
As I do not know how your directories are organized I can not help you with that part.
Good luck.

by ramibekhit

Why ?

Why do you want to print this manual ?

I would suggest that you actually want to export it to PDF or HTML and let the user deal with it.

Whatever printing features you (spend time) build(ing), they will be bettered by those available to HTML and even PDF.

You then save development time and yet deliver more to your customer.

Thanks

by sl-campbell

Add the below function to the declarations section of a module:

'Will print the file using the application that is the default type for the file name extension

Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation _
As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
Private Const SW_SHOWMINNOACTIVE = 7

'On the click event of the command button pass the file path and name to the below API call using this variable: sFilePathAndName.
(You could also add the below to a function and pass the file name to it for each file to be printed.)

ShellExecute MEZ_000.hwnd, "Print", sFilePathAndName, ByVal 0&, ByVal 0&, SW_SHOWMINNOACTIVE

I would suggest using FSO to see first6 if the file exists and also add an error handler to the function that calls the API:

Public Function Fp_iFileExists(ByVal strPathName As String) As Integer
Dim Fso As New Scripting.FileSystemObject
Fp_iFileExists = Fso.FileExists(strPathName)
Set Fso = Nothing
End Function

'Usage: If Fp_iFileExists("C:\myfile") then

This discussion is closed to new replies.