Solved

Select printer and print userform

Posted on 2012-12-21
9
6,116 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

929 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now