Solved

Select printer and print userform

Posted on 2012-12-21
9
5,933 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

762 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

21 Experts available now in Live!

Get 1:1 Help Now