print excel file (1 Viewer)

Dinus

the
Local time
Today, 18:04
Joined
Sep 1, 2005
Messages
17
Hello, I'm back again.

I have this form with an button on it, but I like to know how to use the button for printing an Excel file.

Tnx,

Dinus.
 

Dalorax

Registered User.
Local time
Today, 11:04
Joined
Aug 8, 2005
Messages
12
example

Try this:

The Excel Object Library must be checked in your References for the following generic code to work.

PHP:
Private Sub Command0_Click()
    Dim objXLApp As Excel.Application
    Dim objXLBook As Excel.Workbook
    Dim objDataSheet As Excel.Worksheet
    
    
    
    Set objXLBook = GetObject(Application.CurrentProject.Path & "\ExcelFile\FileName.xls")
    
    Set objXLApp = objXLBook.Parent
    Set objDataSheet = objXLBook.Worksheets("Sheet1")
    
    objDataSheet.PrintOut Copies:=1, Collate:=True
        
    objXLBook.Close savechanges:=False 'close without changes

   
    Set objDataSheet = Nothing
    Set objXLBook = Nothing
    Set objXLApp = Nothing
    
End Sub
 

ghudson

Registered User.
Local time
Today, 12:04
Joined
Jun 8, 2002
Messages
6,195
You can use the PrintFile option of the ShellExecute() method.

My sample shows how to open a file, just rename the OpenFile stuff to PrintFile to do what you want.

Browse [Find a directory or file]
 

Oldsoftboss

AWF VIP
Local time
Tomorrow, 02:04
Joined
Oct 28, 2001
Messages
2,499
ghudson,

I have been requested to construct a db that stores info and a path to drafting drawing files. I have been able to select and open files with the code in your demo, but thought it would be good to have a button that simply prints the file, rather than having to open them first.

I tried changing the "Open" command to "Print" as suggested above without success. Could you explain a little further which code I should be altering.

Dave

http://www.access-programmers.co.uk/forums/showthread.php?p=440522#post440522
 

ghudson

Registered User.
Local time
Today, 12:04
Joined
Jun 8, 2002
Messages
6,195
Just change all of the "open" code to "print". I also changed the "1" parameter to "0" to not display the file being printed.
Code:
Public Function PrintFile(sFileName As String)
On Error GoTo Err_PrintFile

    PrintFile = ShellExecute(Application.hWndAccessApp, "Print", sFileName, "", "C:\", 0)

Exit_PrintFile:
    Exit Function

Err_PrintFile:
    MsgBox Err.Number & " - " & Err.Description
    Resume Exit_PrintFile

End Function
 
Last edited:

Users who are viewing this thread

Top Bottom