Results 1 to 9 of 9

Thread: [RESOLVED] How to set printer through VB

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Resolved [RESOLVED] How to set printer through VB

    I would like set the printer device through VB. How I can do that.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to set printer through VB

    Printer.Devicename = "HP Laserjet"

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: How to set printer through VB

    I want to show the printer dialog and then set the name in the textbox. Actually I don't want to print. I just to set only.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: How to set printer through VB

    I able to populate the printer devices into combo box. The problem is how I can set the printer devices as default?

    Code:
    Private Sub Form_Load()
    'Add all the available printers to the combo box
    For i% = 0 To Printers.Count - 1
        Combo1.AddItem Printers(i%).DeviceName
    Next i%
    
    'Display the default printer
    For i% = 0 To Combo1.ListCount - 1
    If Combo1.List(i%) = Printer.DeviceName Then
    Combo1.ListIndex = i%
    Exit For
    End If
    Next i%
    
    End Sub

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to set printer through VB

    in the combo click event
    vb Code:
    1. dim p as printer
    2. for each p in printers
    3. if p.devicename = combo1.text then set printer = p: exit for
    4. next
    note this does not change the windows default printer, it sets the printer your program will use
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: How to set printer through VB

    Why it does't change the printer default in control panel?

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to set printer through VB

    because that is not what it does, if you want to change the windows default printer, you can use the SetDefaultPrinter api, do a search for that, in general you should change the default printer back, when you finish with it
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: How to set printer through VB

    Nevermind, I will see it later. Let try your sample first. Let say I have chose the printer from combo list and want to use this printer for printing. How to print on paper using the selected printer?

    I want to see how it works to print it on paper by the selected printer of choice and which printer name it will used to print the paper.

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to set printer through VB

    vb Code:
    1. printer.print "this is a test"  ' print something
    2. printer.enddoc   ' finish

    set the printer you want to print to first
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width