+ Reply to Thread
Results 1 to 11 of 11

Print from Userform with Selected Printer

Hybrid View

indub Print from Userform with... 03-19-2015, 02:18 PM
indub Re: Print from Userform with... 03-19-2015, 04:12 PM
indub Re: Print from Userform with... 03-23-2015, 01:51 PM
indub Re: Print from Userform with... 03-23-2015, 02:28 PM
indub Re: Print from Userform with... 04-03-2015, 02:24 PM
gmr4evr1 Re: Print from Userform with... 04-03-2015, 02:29 PM
indub Re: Print from Userform with... 04-04-2015, 11:56 PM
gmr4evr1 Re: Print from Userform with... 04-06-2015, 11:16 AM
SDdeRuiter Re: Print from Userform with... 05-18-2017, 08:53 AM
indub Re: Print from Userform with... 05-18-2017, 11:05 AM
gmr4evr1 Re: Print from Userform with... 05-18-2017, 10:00 AM
  1. #1
    Registered User
    Join Date
    03-19-2015
    Location
    Colorado
    MS-Off Ver
    2010
    Posts
    49

    Print from Userform with Selected Printer

    Hello,

    I am trying to print an excel userform to a specific printer instead of the default printer. Currently I double click any row of data on the worksheet and the userform will open with that row data to the corresponding userform labels as captions. I have a print button on the form using UserForm.PrintForm which prints it out to the default printer without issue. I now have a thermal printer for labels which I'd like to print the userform to instead, but I don't want the thermal printer as my default printer. Is there a way to specify a printer for a userform?

    I don't want to send a printscreen of the userform to another worksheet just to print it to the thermal printer, that would defeat the purpose of even using the userform in the first place. I'm really just trying to add an extra step to UserForm.PrintForm for my print commandbutton so I can select the correct printer. If you have any other suggestions please let me know.

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    03-19-2015
    Location
    Colorado
    MS-Off Ver
    2010
    Posts
    49

    Re: Print from Userform with Selected Printer

    It doesn't look like there's a direct way of doing this via PrintForm since PrintForm defaults to the control panel's default printer and appears to be a standalone function. Is there instead a way to make the "print" commandbutton on the userform change the default printer and then change it back after the userform prints?

  3. #3
    Registered User
    Join Date
    03-19-2015
    Location
    Colorado
    MS-Off Ver
    2010
    Posts
    49

    Re: Print from Userform with Selected Printer

    Well I decided to just make my default printer the thermal printer so the userform prints without issue as a label, and other programs still allow the choice of printer before printing...except for other Microsoft programs like IE. If I leave the default printer as the thermal and print something from IE, even if I select a different printer, it keeps the page size as the thermal size. Based on that, its obviously all connected to windows API.

    I went ahead and coded excel's vba to take a screenshot of the userform, paste on a new page and print to a desired printer, but the window of the userform prints as well. Also, despite having FitToPage set at 1, the print is very small. Here's the current code I have for my userform commandbutton that prints:

    Private Sub CommandButton1_Click()
        Dim strPrintArea
        DoEvents
        keybd_event VK_LMENU, 0, KEYEVENTF_EXTENDEDKEY, 0
        keybd_event VK_SNAPSHOT, 0, KEYEVENTF_EXTENDEDKEY, 0
        keybd_event VK_SNAPSHOT, 0, KEYEVENTF_EXTENDEDKEY + _
            KEYEVENTF_KEYUP, 0
        keybd_event VK_LMENU, 0, KEYEVENTF_EXTENDEDKEY + _
            KEYEVENTF_KEYUP, 0
        DoEvents
        Workbooks.Add
        Application.Wait Now + TimeValue("00:00:01")
        ActiveSheet.PasteSpecial Format:="Bitmap", Link:=False, _
            DisplayAsIcon:=False
        ActiveSheet.Range("A1").Select
        ActiveSheet.PageSetup.Orientation = xlPortrait
        
       
    With ActiveSheet.PageSetup
            .PrintTitleRows = ""
            .PrintTitleColumns = ""
        End With
    
        ActiveSheet.Shapes(1).Select    'Will always be Shape 1
         
        With Selection  'Get print area of picture
            strPrintArea = .TopLeftCell.Address & ":" & .BottomRightCell.Address
        End With
         
        With ActiveSheet.PageSetup
            .PrintTitleRows = ""
            .PrintTitleColumns = ""
        End With
         
        With ActiveSheet.PageSetup
            .PrintArea = strPrintArea  'Set Print Area
            .PrintGridlines = False
            .CenterHorizontally = True
            .CenterVertically = True
            .Orientation = xlPortrait
            .Zoom = False
            .FitToPagesWide = 1
            .FitToPagesTall = 1
            .ScaleWithDocHeaderFooter = True
            .AlignMarginsHeaderFooter = True
        End With
        Application.Dialogs(xlDialogPrinterSetup).Show
        ActiveWindow.SelectedSheets.PrintOut Copies:=1
        ActiveWindow.Close False
    End Sub
    How can I make the either the screenshot or the pasted image of the screenshot only print the non-window userform? Similar to what userform.printform does.

    Also, how can I scale the image to fit the correct paper size since FitToPage isn't working right?

  4. #4
    Registered User
    Join Date
    03-19-2015
    Location
    Colorado
    MS-Off Ver
    2010
    Posts
    49

    Re: Print from Userform with Selected Printer

    Disregard the above code, it works for screenshot but the coding part is ridiculous to do what I want. I instead just found this online, which works perfect! The following code is pasted into the commandbutton's sub, but do not use Option Explicit. You must also have Microsoft word to use this:

    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)
        CommandButton1.Visible = False
        UserForm1.PrintForm
        CommandButton1.Visible = True
        Unload UserForm1
        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
    The only problem with this is it changes the default printer to whatever you select. I plan on using this for all my userforms so it'll change the default accordingly, but if you know of a way within the code above to make it change back to the original default printer. Please let me know as I would prefer that.

  5. #5
    Registered User
    Join Date
    03-19-2015
    Location
    Colorado
    MS-Off Ver
    2010
    Posts
    49

    Re: Print from Userform with Selected Printer

    I need some help regarding the Word portion of the code above.

    My computer started running very slow today so I checked the active processes and WINWORD.EXE was listed hundreds of times. Apparently after printing userforms using the code above it leaves an open process for Word. What can I add to the 'oWord' code portion above so the process ends once the userform prints?

  6. #6
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: Print from Userform with Selected Printer

    Would Exit Sub or Kill oWord work?
    1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG
    You don't have to add Rep if I have helped you out (but it would be nice), but please mark the thread as SOLVED if your issue is resolved.

    Tom

  7. #7
    Registered User
    Join Date
    03-19-2015
    Location
    Colorado
    MS-Off Ver
    2010
    Posts
    49

    Re: Print from Userform with Selected Printer

    Thanks for the suggestions. Those two in particular didn't work, but it put me on the right track. I place 'oWord.Quit' a line above 'oWord = Nothing' and it ends the word process after printing.

  8. #8
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: Print from Userform with Selected Printer

    Glad you were able to get it worked out.

  9. #9
    Registered User
    Join Date
    05-16-2017
    Location
    NL
    MS-Off Ver
    2013
    Posts
    3

    Re: Print from Userform with Selected Printer

    I copied code above but still getting error:

    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)
        CommandButton1.Visible = False
        CommandButton2.Visible = False
        UserForm1.PrintForm
        CommandButton1.Visible = True
        CommandButton2.Visible = True
        Unload Cat1_Rapportage
        ChangeDefaultPrinter (sPrinter)
    
    End If
    
    End Sub
    
    Public Sub ChangeDefaultPrinter(pName As String)
        
        Set oWord = CreateObject("Word.Application")
    oWord.WordBasic.FilePrintSetup Printer:=pName, DoNotSetAsSysDefault:=0
    oWord.Quit
    Set oWord = Nothing
    
       
    End Sub
    Fault tracing ends on line:

    oWord.WordBasic.FilePrintSetup Printer:=pName, DoNotSetAsSysDefault:=0
    telling me Fault 1120 Printerfault.
    What could be wrong?

  10. #10
    Registered User
    Join Date
    03-19-2015
    Location
    Colorado
    MS-Off Ver
    2010
    Posts
    49

    Re: Print from Userform with Selected Printer

    telling me Fault 1120 Printerfault.
    What could be wrong?
    Unfortunately, I'm not sure. It may depend on the version of excel and word you have, you need both installed. I am still using this method on multiple sheets with Excel and Word 2010, works great. As the other poster mentioned, it probably will be best for you to post your question as a new thread to get more traffic.

  11. #11
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: Print from Userform with Selected Printer

    I think it's against forum rules to "piggy-back" on a thread. You should create a new thread with your question. Doing this will also get more views as the only ones that will se this thread are the ones that were involved in it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 01-20-2014, 07:10 PM
  2. How Do I Code to print to a Printer by the Printer Name?
    By rkjudy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-13-2013, 05:28 PM
  3. Specify pdf filename if pdf printer was selected
    By coreytroy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-07-2013, 04:37 PM
  4. VBA code to print Userform to specific Printer
    By RossN in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-08-2012, 06:03 PM
  5. macro to print 2 copies to default printer, then 1 copy to Adobe PDF printer
    By jwright650 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-26-2011, 01:07 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1