Solved

Select printer and print userform

Posted on 2012-12-21
9
6,620 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
Industry Leaders: 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!

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

726 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