VBA Print code help!

M

Mike Yorke

Hi,

I have found this piece of code which will print each
section of a document as a seperate print job.

Dim i As Long
For i = 1 To ActiveDocument.Sections.Count
ActiveDocument.PrintOut Range:=wdPrintFromTo,
From:="s" & i, To:="s" & i
Next i

This works fine except as soon as the macro is executed
the print job is sent to my default printer.

What would be excellent is to see the regular print
dialogue box so I can choose another printer if I wish.
Can the code be tweaked to include this option? Can
anyone help me get started with this?

Thanks for your time,

Mike.
 
J

Jonathan West

Mike Yorke said:
Hi,

I have found this piece of code which will print each
section of a document as a seperate print job.

Dim i As Long
For i = 1 To ActiveDocument.Sections.Count
ActiveDocument.PrintOut Range:=wdPrintFromTo,
From:="s" & i, To:="s" & i
Next i

This works fine except as soon as the macro is executed
the print job is sent to my default printer.

What would be excellent is to see the regular print
dialogue box so I can choose another printer if I wish.
Can the code be tweaked to include this option? Can
anyone help me get started with this?

Thanks for your time,

Mike.

Hi Mike

Add the following code to the start of your routine to allow the user to
select the printer to be used.

With Dialogs(wdDialogFilePrintSetup)
If .Display = -1 Then
.DoNotSetAsSysDefault = True
.Execute
End If
End With
 
G

Guest

Hi Jonathan,

Thanks for replying to me request for help!

The box which appears (wdDialogFilePrintSetup) wasn' the
one I was thinking of. I was expecting to see the
regualar print dialog box, the one which appears when you
click FILE > PRINT.

Is the wdDialogFilePrintSetup box for changing defaults?

In my naevity I changed the code to read
wdDialogFilePrint but this didn't work when ran. Also,
when I clicked Cancel to remove this box the rest of the
code continued and printed my document.

Is it possible to have the regular print box appear, so I
can select me chosen printer, and/or cancel the print
task if necessary.

Cheers

Mike.
 
J

Jonathan West

The thing about the regular File Print dialog is that it covers all kinds of
options that you are overriding anyway, and so there seems little point in
displaying them. For instance, you are printing each page separately, and
always printing every page, so there is no point in having the page range
options displayed. Unless of course, you are wanting tontrol ofver this, in
which case things get rather more complex.

As for wanting to be able to cancel the print job, again, you hadn't
mentioned that you wanted to do this.

It would be worth backing up a bit and describing in more detail what you
want.

- what aspects of the printout do you want to control
- at what point do you want to be able to cancel it
 
G

Guest

Jonathan,

Yes I guess you are right, I don't need to format
anything with the document. I just want to be able to
choose which printer the document goes to.

When I ran the piece of code you provided I wasn't sure
what this Printer Setup box was doing. It seemed to be a
box for changing a users default printer (which is
something I don't want users doing). Also, the only
button on the box was a "cancel" button which confused me
a little. (Maybe it's my lack of understanding about
what this box is for). After playing around with it a
little I can see that I am supposd to highlight the
printer I want to the job to print on and click cancel.
Is this how you were expecting it to work? It doesn't
seems very intuitive though.

I guess I am after something which works the same as this
Print Setup box except doesn't contain any mention of
changing your default and has a button labelled PRINT or
OK instead of CANCEL. My users would get confused!

Mike.
 
J

Jonathan West

Hi Mike,

If you either double-click on the printer dialog or click once and click the
Set as DefaultPrinter button, the Cancel button changes into a Close button.

My code *does not change the system default printer* merely the default for
the current Word session.

To get the opportunity to cancel the print, you combine the two pieves of
code in this way

Dim i As Long
With Dialogs(wdDialogFilePrintSetup)
If .Display = -1 Then
.DoNotSetAsSysDefault = True
.Execute
For i = 1 To ActiveDocument.Sections.Count
ActiveDocument.PrintOut Range:=wdPrintFromTo, _
From:="s" & i, To:="s" & i
Next i
End If
End With

Using the print setup dialog allows the user to change the settings of the
printer as well as select the printer.

If all you want is to select the printer and either continue or cancel, then
you would be better creating your own userform. You can load a list of
available printers into a listbox on the form by the means described in this
article

Getting names of available printers
http://word.mvps.org/FAQs/MacrosVBA/AvailablePrinters.htm

Doing this will allow you to set the user interface to exactly how you want
it to avoid confusion among your users.

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
 

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

Top