Solved

Select printer and print userform

Posted on 2012-12-21
9
6,493 Views
Last Modified: 2012-12-27
Sub CommandButtomln13_Click()
    Dim fileOK As Boolean
    Dim sPrinter As String
    
    With Application
        sPrinter = .ActivePrinter
        fileOK = .Dialogs(xlDialogPrint).Show
    End With
 
    If fileOK = True Then
       Me.PrintForm
       Application.ActivePrinter = sPrinter
    End If
End Sub

Open in new window


Hi,

I have the above code under a button click sub. The purpose is to print the particular userform (the button is on)

I am showing printer dialog in an effort to switch printers should it be required. However, its not working. Regardless of what printer I choose, the userform is being printed on my default printer.

Any help is appreciated (no API calls please as user could be 32/64 bit)

Thanks!
0
Comment
Question by:Shanan212
  • 4
  • 3
  • 2
9 Comments
 
LVL 8

Expert Comment

by:Barry62
ID: 38713505
try reversing the printform and activeprinter statements:

If fileOK = True Then
       Application.ActivePrinter = sPrinter
       Me.PrintForm
    End If

Open in new window

0
 
LVL 13

Author Comment

by:Shanan212
ID: 38713584
No use Barry,

I selected PDF printer after inserting that code and it printed to my physical printer.

Thanks though!
0
 
LVL 8

Expert Comment

by:Barry62
ID: 38713654
try xlDialogPrinterSetup
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 13

Author Comment

by:Shanan212
ID: 38713670
Nope, no help. Is there anyway to capture the printer selected?

As an example

    Application.Dialogs(xlDialogInsertPicture).Show
    Set MyPicture = Selection

Open in new window


I tried selection (msgbox selection) but gave an error in terms of getting printer name
0
 
LVL 8

Expert Comment

by:Barry62
ID: 38713793
Set Application.Printer = Application.Printers(sPrinter)
0
 
LVL 13

Author Comment

by:Shanan212
ID: 38723765
I am getting an error 438
Object doesn't support this property or method on the line
0
 
LVL 9

Accepted Solution

by:
shorvath earned 500 total points
ID: 38725206
Shanan212

I know this sounds lame but you can't change the default printer using Excel VBA.

However, you can change it using Word VBA  (don't ask me, ask Microsoft)
http://support.microsoft.com/kb/216026

This code does what you want and has no API calls, but you do have to have Microsoft Word installed as well as Excel.  (Most user have both)

Private Sub CommandButton1_Click()
Dim fileOK As Boolean
Dim sPrinter As String

With Application
    sPrinter = .ActivePrinter
    fileOK = .Dialogs(xlDialogPrinterSetup).Show
End With

If fileOK = True Then
   ChangeDefaultPrinter (Application.ActivePrinter)
   Me.PrintForm
   ChangeDefaultPrinter (sPrinter)
End If

End Sub


Public Sub ChangeDefaultPrinter(pName As String)

Set oWord = CreateObject("Word.Application")
oWord.WordBasic.FilePrintSetup Printer:=pName, DoNotSetAsSysDefault:=0
Set oWord = Nothing
   
End Sub

Open in new window

0
 
LVL 13

Author Closing Comment

by:Shanan212
ID: 38725237
Thanks it worked. Only thing is that 'oWord' is not defined

Public Sub ChangeDefaultPrinter(pName As String)
    Dim oWord As Object
    Set oWord = CreateObject("Word.Application")
    oWord.Visible = False
    oWord.WordBasic.FilePrintSetup Printer:=pName, DoNotSetAsSysDefault:=0
    Set oWord = Nothing
   
End Sub

Open in new window

0
 
LVL 9

Expert Comment

by:shorvath
ID: 38725547
Oops, sorry,  not everyone uses Option Explicit when coding...
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question