+ Reply to Thread
Results 1 to 14 of 14

Code To Adjust With Changing Printer Location

Hybrid View

  1. #1
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Question Code To Adjust With Changing Printer Location

    My code contains a line in where the name of a specified printer that needs to be used is located on Ne03. However, I've come across this several times where the Ne number will change for some reason unknown to me - in my case, it has now changed to Ne01, meaning all the workbooks that have been saved with Ne03 in the code are no longer working.

    I'm looking for a workaround for this problem where the code would just take the name of the printer (which won't be changing as far as I can tell) regardless of it's ending and just print, but I'm not sure how to go about this in my case. Here is my code:

    Sub Final_Click()
    
    Application.ScreenUpdating = 0
    Sheets("Packaging Label").Visible = 1
    Dim sCurrentPrinter As String
    Const MyPrinter As String = "Brother QL-1060N on Ne03:"
    sCurrentPrinter = Application.ActivePrinter
    Application.ActivePrinter = MyPrinter
    
    On Error Resume Next
    
    y = WorksheetFunction.Find("-", Cells(4, 3))
    If y = Empty Then
        Label = Cells(3, 3) & "-" & Cells(4, 3)
        Sheets("Packaging Label").[d4] = Label
        Sheets("Packaging Label").PrintOut Copies:=Sheets("Entry Page").Range("C22").Value
    Else
        lfrom = Left(Cells(4, 3), y - 1)
         'lto = Right(Cells(4, 3), 4)
        lto = Mid(Cells(4, 3), y + 1)
        For lst = lfrom To lto Step 10
            Label = Cells(3, 3) & "-" & Right("0000" & lst, 4)
             ''''
            If Sheets("Packaging Label").Range("D4").Value <> "" Then
            If Sheets("Packaging Label").Range("D5").Value <> "" Then
            If Sheets("Packaging Label").Range("D6").Value <> "" Then
            If Sheets("Packaging Label").Range("D7").Value <> "" Then
            If Sheets("Packaging Label").Range("D8").Value <> "" Then
            Sheets("Packaging Label").[d4] = Label
            Sheets("Packaging Label").PrintOut
    Else
        MsgBox "Make sure all data is entered before printing labels!", Title:="Attention!"
        
    End If
    End If
    End If
    End If
    End If
    
    Next
    End If
    Application.ActivePrinter = sCurrentPrinter
    Sheets("Packaging Label").Visible = xlSheetHidden
    Application.ScreenUpdating = 1
    End Sub
    Any ideas here would be greatly appreciated. Thanks in advance!
    There is so much good in the worst of us,
    And so much bad in the best of us,
    That it hardly behooves any of us
    To talk about the rest of us.

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Code To Adjust With Changing Printer Location

    This is one of my pet peeves, you don't need the printer port in Word, but you do in Excel. This is the most efficient way of doing it, put the below in a module somewhere:
    Public Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias _
    "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _
    ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
    
    Public Declare Function RegQueryValueEx Lib "advapi32.dll" Alias _
    "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
    String, ByVal lpReserved As Long, lpType As Long, lpData As Any, _
    dwSize As Long) As Long
    
    Public Declare Function RegCloseKey Lib "advapi32.dll" _
    (ByVal hKey As Long) As Long
    
    Private Const dhcKeyAllAccess = &H2003F
    Private Const HKEY_CURRENT_USER = &H80000001
    Private Const dhcRegSz As String = 1
    
    Function GetPrinterPort(PrinterName As String) As String
        Dim hKeyPrinter As Long
        Dim lngResult As Long
        Dim strBuffer As String
        Dim cb As Long
        
        lngResult = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\Microsoft\Windows NT\CurrentVersion\Devices", 0&, dhcKeyAllAccess, hKeyPrinter)
        
        If lngResult = 0 Then
            strBuffer = Space(255)
            cb = Len(strBuffer)
            
            lngResult = RegQueryValueEx(hKeyPrinter, PrinterName, 0&, dhcRegSz, ByVal strBuffer, cb)
    
            If lngResult = 0 Then GetPrinterPort = Right(Left(strBuffer, cb), 6)
            
            lngResult = RegCloseKey(hKeyPrinter)
        End If
    End Function
    This is then called like this:
    strPrinterPort = GetPrinterPort("\\KNV-PRT-P0003\PRT-TOSH-Q-01")

  3. #3
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Code To Adjust With Changing Printer Location

    Thank you Kyle123 - I'm having a few issues getting this to work with my code - where do I put that second code in mine?

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Code To Adjust With Changing Printer Location

    Something like this:
    Application.ActivePrinter = "Brother QL-1060N " & GetPrinterPort("Brother QL-1060N")
    To see what the function returns without running all the code, in the immediate window type:
    ?GetPrinterPort("Brother QL-1060N")

  5. #5
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Code To Adjust With Changing Printer Location

    I'm getting an error - do you have any idea what I'm doing wrong here?

    Application.ScreenUpdating = 0
    Sheets("Packaging Label").Visible = 1
    Dim sCurrentPrinter As String
    sCurrentPrinter = Application.ActivePrinter
    Application.ActivePrinter = "Brother QL-1060N " & GetPrinterPort("Brother QL-1060N")

  6. #6
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Code To Adjust With Changing Printer Location

    missed the on out, try this :
    Application.ActivePrinter = "Brother QL-1060N on " & GetPrinterPort("Brother QL-1060N")
    What do you get when you run ?GetPrinterPort("Brother QL-1060N") in the imediate window?

  7. #7
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Code To Adjust With Changing Printer Location

    Hey! That seems to be doing it. And this may sound like a really dumb question - but how do I run ?GetPrinterPort("Brother QL-1060N") in the immediate window?

  8. #8
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Code To Adjust With Changing Printer Location

    have you tried typing it in.....?

  9. #9
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Code To Adjust With Changing Printer Location

    Um, haha, yes I have, but where? Typing it in the VBA window and then trying to run it gives me an error.

  10. #10
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Code To Adjust With Changing Printer Location

    here:
    \1
    Attached Images Attached Images

  11. #11
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Code To Adjust With Changing Printer Location

    There's a window called Immediate - who would have guessed!

    Okay, so trying to run that brings up the Macros window.

  12. #12
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Code To Adjust With Changing Printer Location

    type it in and just press enter at the end of the line to run it.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  13. #13
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Code To Adjust With Changing Printer Location

    Oh! Thank you. I get Ne03: when I do that. This is very nice, thank you so much!

  14. #14
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Code To Adjust With Changing Printer Location

    Glad it's working, thanks for the feedback

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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