• VB/VBA Call to determine printer type? (Word 2000)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » VB/VBA Call to determine printer type? (Word 2000)

    Author
    Topic
    #358479

    Hi: I have a question; one that I cannot seem to find any information on anywhere … I feel it is a common-enough problem that many firms would have … It has to do with printing different paper types to different printers

    OK, in my office I have several different types of HP printers: HP4+, HP5, HP4050. The only thing that is consistent about these printers is that they have the following tray setups: envelope feeder, MP/Tray1, Tray2, and Tray3.

    I have more paper types than I do trays: draft, bond, 1stpageletterhead, 2ndpageletterhead, legal, labels, envelopes

    So: I would like to standardize what trays will hold what type of paper:

    Envelopes Envelope Feeder
    MP/Tray1 2nd page letterhead
    Tray2 1st page letterhead
    Tray3 Draft

    MP/Tray1 Legal, labels, etc.
    (prompted)

    I want to create a macro that will set the proper paper trays depending on the type of document produced: e.g. “Letter” would set the first page of the document to Tray2 and all subsequent pages to Tray3.
    OK, I can do that. However, the macro only works for HP4050 printers. HP5 printers don’t have a “Tray3”, they have “Lower Bin”.

    Now that I got that all out of the way, here is what I want: A macro that determines the “type” of printer (HP4+, HP5), and then sets the appropriate tray codes. I can get the tray codes my creating the macro for just one printer at a time (whatever my default printer is). What I can’t seem to figure out is whether or not I can tell what type of printer my default printer is, so that I can use a SELECT CASE or IF/THEN statement to set the tray codes. Is there a VB or VBA call that will return this information?

    Thanks for whatever help you can give …

    Andrew Harrell (VBA newbie)
    Greenberg, Traurig
    harrella@gtlaw.com

    Viewing 1 reply thread
    Author
    Replies
    • #535053

      Okay, just for historical reference, I found this code I used in a very old version of Word, probably Word95 (as interpreted by Word2000):

      Dim fps2 As Object
      Set fps2 = WordBasic.DialogRecord.FilePrintSetup(False)
      WordBasic.CurValues.FilePrintSetup fps2
      curPrinter$ = fps2.Printer
       
      Rem Get printer [driver] type (i.e., 4 or 4si) from the local Registry
      curPtrRegKey$ = _
          "HKEY_LOCAL_MACHINESystemCurrentControlSetcontrolPrintPrinters" + _
      	curPrinter$
      curPtrType$ = _
          WordBasic.[GetPrivateProfileString$](curPtrRegKey$, "Printer Driver", "")

      Lovely, eh? To do this in VBA would be:

      Sub DiscoverPrinter()
      Dim strPrinter As String, strDriver As String
      'retrieve full printer name and port string
      strPrinter = Application.ActivePrinter
      'strip port information
      strPrinter = Left(strPrinter, InStr(1, strPrinter, " on ", vbBinaryCompare) - 1)
      'look up driver name
      strDriver = System.PrivateProfileString("", "HKEY_LOCAL_MACHINESystem" & _
              "CurrentControlSetcontrolPrintPrinters" & strPrinter, _
              "Printer Driver")
      'report findings
      MsgBox strPrinter & " is a(n) " & strDriver
      End Sub

      Hope this helps, and please post back with the working solution; you will earn lots of fans! (And then I won’t feel guilty about helping out the competition. wink)

      • #535190

        Will be happy to do so … will need to get with some of my more-experienced colleagues to apply … but will post back.

        Many Thanks!!

      • #535204

        crybaby
        Close, but no cigar on our system… we’re running Win2k on Win2k Workstations and Win2k server…

        Any suggestions?

        • #535281

          Oh, man. After an hour in the Registry it’s time to go home! Here is a version that works with Windows 2000 Professional on the one PC I could try it on:

          Sub DiscoverPrinter()
          Dim strPrinter As String, strDriver As String, strServer() As String
          'retrieve full printer name and port info
          strPrinter = Application.ActivePrinter
          'strip port information from printer name (necessary for Win98)
          strPrinter = Left(strPrinter, InStr(1, strPrinter, " on ", vbBinaryCompare) - 1)
          'look up driver name
          strDriver = System.PrivateProfileString("", "HKEY_LOCAL_MACHINESystem" & _
                  "CurrentControlSetcontrolPrintPrinters" & strPrinter, _
                  "Printer Driver")
          'if driver name is blank, try key for Windows2000
          If strDriver = vbNullString Then
              'parse server name from share name
              strServer = Split(strPrinter, "", , vbTextCompare)
              strDriver = System.PrivateProfileString("", "HKEY_LOCAL_MACHINESoftware" & _
                  "MicrosoftWindows NTCurrentVersionPrintProvidersLanMan Print Services" & _
                  "Servers" & strServer(2) & "Printers" & strServer(3), "Printer Driver")
          End If
          'report findings
          MsgBox strPrinter & " is a(n) " & strDriver
          End Sub

          Let me know how it works out.

          • #535384

            Hey, thanks for the effort and the code! It did work — I found the same registry data earlier in the evening, and fumbled thru getting the strDriver parsed to get to the share name (not as elegant as you did it). And it did work!!

            Next step is to put in the conditionals for the printer driver types … will post the code as soon as Karen and I have tested …. thanks again!

          • #537405

            OK, I’ve got the code … many thanks for what would have been the hard part for me to figure out — the registry lookup , etc …

            A little info on the completed macro — I am not an HP printer expert. A big part of getting this macro to work was to check every printer(actually the driver) and then see what settings are made when different printers are used as the default printer.

            Well …. Enjoy, and let me know what you think about my first effort here …….

            ‘*** Document Paper Type Macro ****************************

            ‘Written by: Andrew Harrell on: 8/06/01

            ‘**********************************************************

            Option Explicit ‘turn on required var declaration
            ‘Give the paper types a name; easier to follow code
            Private Enum gtPaperType
            draft = 1
            Letterhead = 2
            Other = 3
            End Enum

            ‘These next three subs form the macs for the toolbar
            Public Sub SelDraftPaper()
            Call DiscoverPrinter(draft)
            End Sub
            Public Sub SelLHPaper()
            Call DiscoverPrinter(Letterhead)
            End Sub
            Public Sub SelOtherPaper()
            Call DiscoverPrinter(Other)
            End Sub

            ‘ **********************************************************
            ‘ Discover Default Printer (by printer driver)
            ‘ **********************************************************
            Private Sub DiscoverPrinter(ByVal intChoice As gtPaperType)
            Dim strPrinter As String, strdriver As String, strServer() As String

            ‘ *** Retrieve full printer name and port string
            strPrinter = Application.ActivePrinter
            ‘ *** Strip port information
            strPrinter = Left(strPrinter, InStr(1, strPrinter, ” on “, vbBinaryCompare) – 1)
            ‘ *** Parse server name from share name
            strServer = Split(strPrinter, “”, , vbTextCompare)
            ‘ *** Look up driver name in Registry
            strdriver = System.PrivateProfileString(“”, “HKEY_LOCAL_MACHINESoftware” & _
            “MicrosoftWindows NTCurrentVersionPrintProvidersLanMan Print Services” & _
            “Servers” & strServer(2) & “Printers” & strServer(3), “Printer Driver”)

            ‘ *** Now call the proper Printer subroutine to set the bins
            Select Case strdriver
            Case Is = “HP LaserJet 5”
            Call HP5BINZ(intChoice)
            Case Is = “HP LaserJet 5N”
            Call HP5BINZ(intChoice)
            Case Is = “HP LaserJet 4 Plus”
            Call HP5BINZ(intChoice)
            Case Is = “HP LaserJet 4000 Series PCL”
            Call HP4000BINZ(intChoice)
            Case Is = “HP LaserJet 4050 Series PCL”
            Call HP4000BINZ(intChoice)
            Case Else
            MsgBox Prompt:=”You must manually set Paper trays” & vbLf & “for this Printer: ” & strdriver, _
            Buttons:=vbOKOnly + vbInformation, Title:=”Set Paper Trays”
            End Select

            End Sub
            ‘ **********************************************************
            ‘ End of Macro
            ‘ **********************************************************

            Private Sub HP5BINZ(ByVal intChoice As gtPaperType)

            ‘ HP5BINZ Macro
            ‘ Macro recorded 8/01/2001 by Harrella

            With ActiveDocument.PageSetup

            Select Case intChoice
            Case Is = gtPaperType.draft
            .FirstPageTray = wdPrinterDefaultBin
            .OtherPagesTray = wdPrinterDefaultBin
            Case Is = gtPaperType.Letterhead
            .FirstPageTray = wdPrinterLowerBin
            .OtherPagesTray = wdPrinterUpperBin
            Case Is = gtPaperType.Other
            .FirstPageTray = wdPrinterManualFeed
            .OtherPagesTray = wdPrinterManualFeed
            End Select

            End With
            End Sub

            Private Sub HP4000BINZ(ByVal intChoice As gtPaperType)

            ‘ HP4000BINZ Macro
            ‘ Macro recorded 8/01/2001 by Harrella

            With ActiveDocument.PageSetup

            Select Case intChoice
            Case Is = gtPaperType.draft
            .FirstPageTray = wdPrinterDefaultBin
            .OtherPagesTray = wdPrinterDefaultBin
            Case Is = gtPaperType.Letterhead
            .FirstPageTray = 260
            .OtherPagesTray = 261
            Case Is = gtPaperType.Other
            .FirstPageTray = 257
            .OtherPagesTray = 257
            End Select

            End With
            End Sub

    • #535404

      API calls should help in identifying printer.

    Viewing 1 reply thread
    Reply To: VB/VBA Call to determine printer type? (Word 2000)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: