Setting the System Default Printer with Visual Basic 6

I know this doesn't exactly qualify as “.NET” knowledge but, it's something I've had to battle with for a while so I thought I would post it in case anyone else finds it as helpful as I have.

How to set which printer is the system default printer

Now, setting a printer to be the system default printer in Visual Basic 6 is way more difficult than it needs to be. If perhaps the DeviceName property of the VB.Printer object wasn't read-only we could set the default printer by passing the name of a printer to that property. Or, in perhaps a more OO approach, It would be nice if there was a “VB.DefaultPrinter” object that we could set equal to a particular VB.Printer object. Something like this would do:

Dim p as VB.Printer
For Each p In VB.Printers
   If p.DeviceName = “ThePrinterIWantToBeTheDefault“ Then
      Set VB.DefaultPrinter = p
   End If
Next

I would at least feel better about writing code like that than having to monkey around with the Win.ini file. Sheesh.

UPDATE: Wow, I really feel stupid now. I spent the better part of an entire workday trying to figure out how to do this when I could have just referenced the Windows Script Host Object Model (/system32/wshom.ocx) and coded this:

Dim w As New WshNetwork
w.SetDefaultPrinter (”ThePrinterIWantToBeTheDefault”)
Set w = Nothing

wow, 3 lines. Much easier. Thank you WSH team.

43 Comments

  • That's what you get for foolin around with VB6!! :-)



    I would assume you wouldn't be if it wasn't for existing code or client requirements.

  • Do you want to set the default printer for *all applications* on the machine?



    To just set the default for your VB app use:-



    Dim p as VB.Printer

    For Each p In VB.Printers

    If p.DeviceName = “ThePrinterIWantToBeTheDefault“ Then

    Set Printer = p

    End If

    Next



  • Duncan,

    Actually - I don't want to set the default printer for all applications - I need to set the default printer to one of any number of printers - depending on which printer was chosen for a certain document. The WSHNetwork object comes in handy.

  • nice one... i really needed this for a long time...

  • Thanks, that's perfect.
    I have a VB6 applicattion, but which uses Word to print and the clients want to be able to select a printer each time.

    This code lets them select a printer for a specific job, and then restores the previous default printer afterwards.

    Dim wsh as new WshNetwork
    strDefault = Printer.DeviceName ' get the default
    CmnDlg.ShowPrinter ' user selects new printer
    ' Word print code goes here
    wsh.SetDefaultPrinter (strDefault) ' restore default

    It muddles technologies, but it works!

  • i have a application that requires a num of printers.
    this code is great thanks.



  • sorry it dint wotk for me. Please assit:

    M setting vb to print a datareport to a printer but it keeps printing to the default.

    Dim p as VB.Printer
    For Each p In VB.Printers
    If p.DeviceName = "LX-300+" Then
    Set Printer = p
    DataReport1.PrintReport 'THIS DOESNT WORK, WHY?
    End If
    Next

    This method works fine when printing a form but not the report. Why?

    Dim p as VB.Printer
    For Each p In VB.Printers
    If p.DeviceName = "LX-300+" Then
    Set Printer = p
    Form1.PrintForm 'THIS WORKS
    End If
    Next



    Please assist.
    eric.

  • hi guys,
    here comes the best solution for changing default printer for reports:

    'simply add Windows Script Host Object Model from Projects>>Refrences or simply scroll for "wshom.dll" then click OK.
    After that paste this code;

    Dim w As New WshNetwork
    w.SetDefaultPrinter ("PrinterName")
    Set w = Nothing
    'the do your printing e.g
    DataReport1.PrintReport


    NB: It works perfect and very fast. Had spend 2 nights desperately looking for a better solution cuz i believed the long API code way provided by Microsoft in the references wasn't the best (it was slowing down my hotel system especially if the pc was slow). So i got this after some research. And it was the best code have ever hustled for so hard. Hope it helps you. Cheers!
    Eric, Nairobi Kenya.


  • Preparing package & Deployment for other computers, with the wshom.ocx, W2K , the older ocx can not replaced because it is renewing by windows .

    The wshom.ocx from 7/2006 does not make the work (of changing default printer) but the new wshom from 8/2004 can not replace the older one.

    Please help!

  • The Windows Script Host Object Model works perfect when the Userlogged is an Administrator, however I got problems when the user account in XP is a limited Account, any help?

  • how to count print page using visual basic 6.0 ..??

    thankz

  • Using the printers collection worked for me.

    I was using the Scripting object which was a nice and neat solution but when I rolled the software out to the clients - it failed with an Automation Error: Exception Occurred error.

    BTW whoever writes error descriptions like "Exception Occurred" needs to be put aginst the wall.

  • Everyone's praising these a lot but didn't anyone find any answers for "Exception Occured" error. I agree Brian it's a very meaningful error.

  • what a great solution to set the default printer.
    lot of thenks Eric. i was looking for this since last week.
    i check it works fine.. thanks again Eric
    from: AJPatel

    hi guys,

    here comes the best solution for changing default printer for reports:

    'simply add Windows Script Host Object Model from Projects>>Refrences or simply scroll for "wshom.dll" then click OK.

    After that paste this code;

    Dim w As New WshNetwork

    w.SetDefaultPrinter ("PrinterName")

    Set w = Nothing

    'the do your printing e.g

    DataReport1.PrintReport

  • Ecellent, just popped it in and works like a champ

  • Thanks! The Windows Script Host Object solution works great!

  • The Windows Script Host Object works like a charm, will see when I deploy though

    Thanks Eric

  • can sum1 please help on printing a pre-printed receipt..cos i have a problem, im using the printer.print method..the continous paper just getting out instead of stopping in the designated page area and size...how can do the right code? in visual basic..please help

  • The Windows Script Host Object solution works like a charm.
    Thanks Eric

  • Hi jamauss
    thanks for a gr8 & easy solution that works butter smooth.

  • how so i set the printer into grayscale only using visual basic 6?? tnx guyz..

  • The wsh works well but not when a user is a limited user account on xp, how do we sort that out? Thanks for the help.

  • TO JASON,

    THANKS FOR THIS VALUABLE TIPS.HOPING FOR NEXT VALUABLES FROM U LIKE GENIUS GUYS

  • Very Helpful...thx

  • Excellent idea..
    It worked out..

    1. Add
    projects->References->Windows Script Host Object Model

    2.
    Dim w As New WshNetwork
    w.SetDefaultPrinter ("PDF995") ' Your printer name
    Set w = Nothing
    Form1.PrintForm

  • Great work
    I am using desired printer other than Default Printer.

  • What your stating is exclusively real. I identify that everybody need to say that the the same element, but I just consider
    you to spot it in a method that everybody can recognize. I also enjoy the photos you spot in here.

    That they in shape so nicely with what youre attempting to say.
    Im particular youll attain so a variety of folks with what youve
    acquired to say.

  • I??m a blog crazed individual and i also for example to read cool blog along the lines of yours.
    ??-,*??

  • Excellent write-up, I’m regular visitor of one’s web site,
    maintain up that the excellent operate, and It’s going to be a regular visitor
    for a long time.

  • This site is really a walk-through for all of the info you wanted about it and didn’t
    recognize who to problem. Glimpse here, and you’ll certainly discover
    it.

  • I wanted to visit and allow that you recognize how great I liked
    searching for your web blog nowadays. I’d consider it that the honor to operate at my place of work and
    have the ability to operate on the tips
    discussed on your website and also be involved in visitors’ responses just like this.
    Should a position connected with guest article author become on
    present at your end, make sure you let me know.

  • This is a fine blog, would you be involved in doing an interview about just
    how you designed it? If so e-mail me!

  • Just desire to say your article is because astounding.
    The clarity in your post is just good and i could assume you have been an expert
    on this subject. Well with your permission allow me to grab your RSS
    feed to keep updated with forthcoming post.
    Thanks a million and please carry on that the enjoyable work.

  • Hello! I only want to make a enormous thumbs up with that the wonderful info you’ve here
    during this post. We have been returning to your blog for additional soon.

  • I’ve been wondering about the similar factor
    myself lately. Delighted to see an individual on that the same wavelength!
    Nice write-up.

  • Thanks for this code, it's still very usefull even in 2013 ;)
    I use it in conjunction with ShellExecute (using the "print" verb) and it works perfectly.
    Thanks a lot.

  • So anyways, I'm a little wiser in my work place and getting into great detail in previous comments worried about your physical activity will also reduce the number of health and longevity. If there's no pain from swimming.
    Go to Xyngular in February that it lost in addition to traditional Chinese medicine to stimulate
    raspberry ketones. It helps your body is a very alarming rate.

  • Green tea raspberry ketones patch has some factors that occur due to an eating disorder,
    mixed with fresh fruit as you do a little weight the soonest time
    possible, always out of the intestine. To go and they are
    hard to stop eating your body. The temporary
    raspberry ketones, one gets the opportunity to seek out women that PCOS can be
    detected as a bona fide hunger.

  • This paragraph provides clear idea in support
    of the new people of blogging, that actually how
    to do blogging and site-building.

  • People who want to have that much from each group ate a plate of intelligently combined
    and perfectly normal body weight than genetics.

    By taking these raspberry ketones are meant to cut back on later.
    A 2004 study published in the process.

  • Incredible quest there. What happened after? Take care!

  • An extract standardized green coffee bean extract for a beneficial group
    of chemicals called polyphenols is made from the dried leaves
    of a shrub called Camellia sinensis.

  • This is done through laxatives, which causes bowel movements.
    2008 Oct 28; 1193:376-412. So never ever use these kinds of methods available over the counter.
    Some of these will include nervousness, anxiety, insomnia and dizziness.
    Although it is among the best Pure Green Coffee Bean Extract
    Reviews.

Comments have been disabled for this content.