• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7564
  • Last Modified:

Select printer and print userform

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
Shanan212
Asked:
Shanan212
  • 4
  • 3
  • 2
1 Solution
 
Barry62Commented:
try reversing the printform and activeprinter statements:

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

Open in new window

0
 
Shanan212Author Commented:
No use Barry,

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

Thanks though!
0
 
Barry62Commented:
try xlDialogPrinterSetup
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Shanan212Author Commented:
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
 
Barry62Commented:
Set Application.Printer = Application.Printers(sPrinter)
0
 
Shanan212Author Commented:
I am getting an error 438
Object doesn't support this property or method on the line
0
 
shorvathCommented:
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
 
Shanan212Author Commented:
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
 
shorvathCommented:
Oops, sorry,  not everyone uses Option Explicit when coding...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now