Avatar of Joel Allen
Joel Allen
 asked on

Outlook VB Script Printer


I have an Outlook custom form that uses VB Script to print a label.  This works fine.  IT has since migrated Office to a virtual server.  Because of this, the issue is that every time you log on to the server, the printer name changes dynamically, like "DYMO (redirected 99)".  The "99" is the part that changes.

Is it possible to call out a printer that contains the name "DYMO" in it?  Here is the code that I use.

Sub CommandButtonShipLabelUSPS_Click()

On Error Resume Next

Dim strCurrentPrinter

      Set objDoc2 = GetWordDocLabel("\\fireglass.com\drawing$\Jobs\Task_Templates\Ship-Ship Label USPS.dot")
          Call FillFieldsShipLabel(objDoc2)
          objDoc2.Application.Options.PrintBackground = False
          objDoc2.Application.DisplayAlerts = False
          'objDoc2.Application.ActiveDocument.AttachedTemplate = "Normal"
          'objDoc2.Application.ActiveDocument.PageSetup.Orientation = 1

if Item.UserProperties("PrintHold") = True then
        Set objDoc2 = Nothing
        Set strCurrentPrinter = Nothing
        Set objWord2 = Nothing
      Exit Sub
end if

          strCurrentPrinter = objDoc2.Application.ActivePrinter ' store the current active printer
          objDoc2.Application.ActivePrinter = "DYMO" ' change to another printer

bjDoc2.PrintOut ' print the active sheet
objDoc2.Application.ActivePrinter = strCurrentPrinter

      objDoc2.Application.DisplayAlerts = True
          objDoc2.Application.Options.PrintBackground = True

objDoc2.Close wdDoNotSaveChanges
objWord2.Quit wdDoNotSaveChanges

      Set objDoc2 = Nothing
        Set strCurrentPrinter = Nothing
        Set objWord2 = Nothing

End Sub

Thank you,
Microsoft OfficeVB ScriptOutlook

Avatar of undefined
Last Comment
Joel Allen

8/22/2022 - Mon

Either fix the code for everyone (or link user to his own shared dymo, works only in LAN environment), or enumerate all, and select based on detecting Dymo in the string  (requires every computer that logs in to NOT have ghost/hidden/inactive Dymo printers in their list):

 For Each prtLoop In Application.Printers 
            With prtLoop 
                strMsg = strMsg _ 
                    & "Device name: " & .DeviceName & vbCrLf _ 
                    & "Driver name: " & .DriverName & vbCrLf _ 
                    & "Port: " & .Port & vbCrLf & vbCrLf 
            End With 
        Next prtLoop 

Open in new window

Example code only, insert an if statement detecting Dymo and setting the printer or other variable.
Joel Allen

Thank you.  I think we're getting close.  I get "Expected end of statement" error on the last line "Next prtLoop".



Open in new window

instead of what I put there (mistake on my side)

Next prtLoop

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Joel Allen

I'm a little confused.  The last line does have Next prtLoop.

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Joel Allen

Thank you.