Open, print and close workbook

D

despistado

I need to open a workbook, print it and close, for example from a .ba
file, but that the user can open the workbook without problem.

I write this:
Private Sub WorkBook_Open()
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
Application.Quit
End Sub

But when I execute excel.exe c:\myworkbook the excel ask my if I wan
to activate/desactivate macros.

Somebody same me that the best option is to create a new workbook an
write
Private Sub Auto_Open()
Workbooks.Open Filename:="c:\example.xls"
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
Application.Quit
End Sub

It's possible to do something like this but "c:\example.xls" will be a
parameter
 
N

Nick Hodge

despistado

The macro warning will always be a problem unless you

1) Get a certificate and sign the code with it
2) Drop the macro security to low, this will leave the application
vulnerable to macro attack. (Although in truth, I don't think I've ever said
no to the pop up)
3) Make the workbook and add-in and load it in advance.

You cannot pass parameters to VBA from the command line but have you
considered having the code workbook open all the time with some code using
the OnTime method of the application object. So you have this in Workbook1
which then opens your print workbook, with no code in, prints it and closes
it. Like so...

Sub RunOnTime()
'Run this once and at the next 9am it will run
'the procedure OpenBook2
Application.OnTime "09:00:00", "OpenBook2"
End Sub

Sub OpenBook2()
'This procedure opens the book2.xls file
'Prints it and closes it without saving
'It then loops back to RunOnTime and runs again
'at the next 9am
Dim wb2 As Workbook
Set wb2 = Workbooks.Open("C:\Book1.xls")
wb2.PrintOut
wb2.Close SaveChanges:=False
RunOnTime
End Sub


--
HTH
Nick Hodge
Microsoft MVP - Excel
Southampton, England
(e-mail address removed)
 
K

keepITcool

OP could create a VBS script.
if allowed to run on the system..then he will NOT get "enable macro
warnings"


--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Nick Hodge wrote :
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Printing Macro 4
Command Button 6
Preventing printing 3
Print Range 1
Checkboxes and Cobinations 1
Print to PDF format 4
Pop Up Message before printing 3
Printing Macro 1

Top