+ Reply to Thread
Results 1 to 17 of 17

VBA Print Sheet crashes.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-28-2020
    Location
    Inverness, Scotland
    MS-Off Ver
    365
    Posts
    308

    VBA Print Sheet crashes.

    I am using the code below to print to the specified printer however
    it is giving an error as attached. Grateful for any help.

    Sub PrintLabel()
    Dim sCurrentPrinter As String
    Const MyPrinter As String = ""
    sCurrentPrinter = Application.ActivePrinter
    Application.ActivePrinter = MyPrinter
    ActiveSheet.PrintOut '
    Application.ActivePrinter = sCurrentPrinter
    End Sub
    Attached Images Attached Images
    Last edited by And180y; 05-10-2020 at 07:41 PM.

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,018

    Re: VBA Print Sheet crashes.

    .
    You can use this to replace your entire macro :

    ActiveSheet.PrintOut
    https://docs.microsoft.com/en-us/off...heets.printout

  3. #3
    Forum Contributor
    Join Date
    02-28-2020
    Location
    Inverness, Scotland
    MS-Off Ver
    365
    Posts
    308

    Re: VBA Print Sheet crashes.

    Thanks mate. I was trying to force the sheet to use only that printer though?

  4. #4
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,018

    Re: VBA Print Sheet crashes.


  5. #5
    Forum Contributor
    Join Date
    02-28-2020
    Location
    Inverness, Scotland
    MS-Off Ver
    365
    Posts
    308

    Re: VBA Print Sheet crashes.

    Thanks for replying. :-)

    Not really mate. Basically there are two printers plugged into the laptop.
    One an A4 and the label printer as above. The idea was that when printing a label (designed in Excel) a button could be pressed to
    in the sheet to send the label to the label printer without having to select it from the print menu.

  6. #6
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,550

    Re: VBA Print Sheet crashes.

    This works for me on my desktop.
    Sub Print_To_LinePrinter()
    Dim STDprinter As String
    STDprinter = Application.ActivePrinter
    Application.ActivePrinter = "HP Officejet Pro 8600 (Network) on Ne03:"
    With Sheets("Blah Blah").PageSetup
        .PrintArea = Range("A1:N36").Address
        .Orientation = xlLandscape
    End With
    Sheets("Blah Blah").PrintOut
    Application.ActivePrinter = STDprinter
    End Sub
    When I need to know the different printers, I go to
    Control Panel - Hardware and Sound - Devices and Printers
    and set the printer in question as default printer and run this
    Sub Get_Printer_Name()
        MsgBox Application.ActivePrinter
    End Sub
    In my case I get
    "Microsoft Print to PDF on Ne01:"
    for saving to PDF
    and
    "HP Officejet Pro 8600 (Network) on Ne03:"
    when I have the line printer as default.

    I am sure you must be able to get a list of all available printers in excel but this works for me.
    Last edited by jolivanes; 05-09-2020 at 10:56 PM. Reason: Add code and info

  7. #7
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,550

    Re: VBA Print Sheet crashes.

    I found this by Rory Archibald.
    Sub PrintersAndPorts()
    
    'This works with Windows 2000 and up
    
       Dim Arr               As Variant
       Dim Device            As Variant
       Dim Devices           As Variant
       Dim msg               As String
       Dim RegObj            As Object
       Dim RegValue          As String
       Const HKEY_CURRENT_USER = &H80000001
    
       Set RegObj = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
       RegObj.enumvalues HKEY_CURRENT_USER, "Software\Microsoft\Windows NT\CurrentVersion\Devices", Devices, Arr
    
       For Each Device In Devices
          RegObj.getstringvalue HKEY_CURRENT_USER, "Software\Microsoft\Windows NT\CurrentVersion\Devices", Device, RegValue
          msg = msg & Device & " on " & Split(RegValue, ",")(1) & vbCrLf
       Next
    
       MsgBox msg, vbInformation, "Printers and Ports"
    
    End Sub
    If you want the result in Column A, starting at cell A2, as Leith Ross did with this.
    Sub PrintersAndPorts()
    
     'This works with Windows 2000 and up
     
      Dim Arr As Variant
      Dim Device As Variant
      Dim Devices As Variant
      Dim Msg As String
      Dim RegObj As Object
      Dim RegValue As String
      Dim R As Long
      Dim Rng As Range
      Dim Wks As Worksheet
      
      Const HKEY_CURRENT_USER = &H80000001
      
        Set Wks = Worksheets("Sheet1")
        Set Rng = Wks.Range("A2")
           
        Set RegObj = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
        RegObj.enumvalues HKEY_CURRENT_USER, "Software\Microsoft\Windows NT\CurrentVersion\Devices", Devices, Arr
        
          For Each Device In Devices
              RegObj.getstringvalue HKEY_CURRENT_USER, "Software\Microsoft\Windows NT\CurrentVersion\Devices", Device, RegValue
              Rng.Offset(R, 0) = Device & " on " & Split(RegValue, ",")(1)
              R = R + 1
          Next
        
    End Sub

  8. #8
    Forum Contributor
    Join Date
    02-28-2020
    Location
    Inverness, Scotland
    MS-Off Ver
    365
    Posts
    308

    Re: VBA Print Sheet crashes.

    # Post 6 worked once and gives me the same error as above thereafter. This is the actual error line in the code; Application.ActivePrinter = "Samsung ML1660 Series (Usb 01) on Ne02:"

    Edit. Post #7 just displayed a load of text in the actual label.
    Last edited by And180y; 05-10-2020 at 03:48 PM.

  9. #9
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,550

    Re: VBA Print Sheet crashes.

    Re: "a load of text in the actual label"
    Maybe an example of the "load of text" might help and what do you consider a "label"? The Message Box as it is referred to in the code I assume.

    So show us the code after you changed it.

  10. #10
    Forum Contributor
    Join Date
    02-28-2020
    Location
    Inverness, Scotland
    MS-Off Ver
    365
    Posts
    308

    Re: VBA Print Sheet crashes.

    Thanks Jolivanes :-) I just read the 2nd Macro again, it is set to send the result to A2 which in my haste I changed to A2:G22. The code was right, I was stoopid. Sorry.

    I have a label designed which sits in the range A22:G22 this is the code that ran once.

    Sub Print_To_LinePrinter()
    Dim STDprinter As String
    STDprinter = Application.ActivePrinter
    Application.ActivePrinter = "Samsung ML1660 Series (Usb 01) on Ne02:"
    With Sheets("Label").PageSetup
        .PrintArea = Range("A2:G22").Address
        .Orientation = xlLandscape
    End With
    Sheets("Label").PrintOut
    Application.ActivePrinter = STDprinter
    End Sub

  11. #11
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,550

    Re: VBA Print Sheet crashes.

    So does it all work now?


    In cases like this, the reference to A2 should be changed to a range where you have free rows below that cell or just run it in a new and empty temp sheet.
    All you need it for is to have a data range with all your printers so you can change your code accordingly.

  12. #12
    Forum Contributor
    Join Date
    02-28-2020
    Location
    Inverness, Scotland
    MS-Off Ver
    365
    Posts
    308

    Re: VBA Print Sheet crashes.

    No still not working. :-(

  13. #13
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,550

    Re: VBA Print Sheet crashes.

    What is the result from the 2nd macro in Post #7?
    Run it on an empty sheet.

  14. #14
    Forum Contributor
    Join Date
    02-28-2020
    Location
    Inverness, Scotland
    MS-Off Ver
    365
    Posts
    308

    Re: VBA Print Sheet crashes.

    Hi Jolvanes, thanks for sticking with this.

    Send To OneNote 2007 on Ne01:
    Samsung ML-1660 Series (USB001) on Ne02:
    PDFCreator on Ne03:
    Microsoft XPS Document Writer on Ne04:
    Microsoft Print to PDF on Ne05:
    Microsoft Office Document Image Writer on Ne06:
    HP3A01E0 (HP Photosmart C4500 series) on Ne06:
    Fax on Ne07:
    Brother QL-570 on Ne09:
    Samsung Universal Print Driver 3 on Ne01:
    ZDesigner LP 2844-Z on Ne00:
    EPSON ET-2650 Series on Ne08:
    Send To OneNote on nul:
    OneNote on Ne10:

  15. #15
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,550

    Re: VBA Print Sheet crashes.

    In Post #10, you have this (Usb 01)
    Application.ActivePrinter = "Samsung ML1660 Series (Usb 01) on Ne02:"
    However, the result from running the code has this (USB001)
    "Samsung ML-1660 Series (USB001) on Ne02:"
    If you change your code to the proper line, exactly as above, does that work.
    Use a copy and paste so you won't type wrong

  16. #16
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,550

    Re: VBA Print Sheet crashes.

    I guess we answered at the same time.
    You've got it licked at that's what count.
    Good Luck and stay safe

  17. #17
    Forum Contributor
    Join Date
    02-28-2020
    Location
    Inverness, Scotland
    MS-Off Ver
    365
    Posts
    308

    Re: VBA Print Sheet crashes.

    Jolivanes, I removed the " " marks from around the printer, the line of code went red but when I put them back in it now works perfectly.
    Not sure what happened but thank you so much for taking the time to help. :-)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. UDF with Intersect in conditional formatting crashes excel during print preview
    By SamPoulson in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-19-2016, 02:19 PM
  2. When trying to print File crashes
    By Dano59 in forum Excel General
    Replies: 3
    Last Post: 01-31-2015, 02:24 AM
  3. [SOLVED] Userform button action - clear,copy paste and print crashes. Please help with code
    By intex in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-05-2014, 12:16 PM
  4. [SOLVED] Excel Crashes when Sheet is Deleted
    By JungleJme in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 08-30-2013, 11:59 AM
  5. excel 2010 crashes when printing and print.preview Macro
    By zmhnk in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-08-2012, 09:24 AM
  6. [SOLVED] My sheet crashes when i close it, due to VBA?
    By galvinpaddy in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 08-21-2012, 05:23 PM
  7. MS EXCEL SHEET CRASHES WHEN VIEWING
    By Wally in forum Excel General
    Replies: 1
    Last Post: 09-07-2005, 03:05 PM

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