Forum Discussion

Moysoul's avatar
Moysoul
Copper Contributor
May 15, 2024
Solved

Printout macros problem

hello friends,

I'm facing an issue when i record an macros to printout sheet using this code 

 

Application.Dialogs(xlDialogPrinterSetup).Show
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False

 

 it's taken an order to printout if i press cancel, i need to solve it to cancel the printout if press cancel so please i need to solve it.

  • Moysoul 

    Let's try a different approach 🙂

    Sub PrintWithPrinterSetup()
        Dim printerDialog As Variant
        
        ' Show printer setup dialog
        Set printerDialog = Application.Dialogs(xlDialogPrinterSetup)
        If Not printerDialog.Show Then
            MsgBox "Printing canceled by user.", vbExclamation
            Exit Sub
        End If
        
        ' Print only if the user didn't cancel
        ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
            IgnorePrintAreas:=False
    End Sub

    If that doesn't work either, please add more detailed information such as Excel version, operating system, storage medium, etc.
    If possible the file (without sensitive data).

4 Replies

  • NikolinoDE's avatar
    NikolinoDE
    Platinum Contributor

    Moysoul 

    The macro does not include a check to see if the user canceled the dialog. To address this, you can maybe modify your code to handle the case where the user cancels the printer setup dialog.

    Here is a modified VBA Code you can try (Code is untested, please backup your file first):

    Sub PrintWithPrinterSetup()
        On Error Resume Next
        Application.Dialogs(xlDialogPrinterSetup).Show
        If Err.Number <> 0 Then
            MsgBox "Printing canceled by user.", vbExclamation
            Err.Clear
            Exit Sub
        End If
        On Error GoTo 0
    
        ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
            IgnorePrintAreas:=False
    End Sub

    Hope this will help you.

      • NikolinoDE's avatar
        NikolinoDE
        Platinum Contributor

        Moysoul 

        Let's try a different approach 🙂

        Sub PrintWithPrinterSetup()
            Dim printerDialog As Variant
            
            ' Show printer setup dialog
            Set printerDialog = Application.Dialogs(xlDialogPrinterSetup)
            If Not printerDialog.Show Then
                MsgBox "Printing canceled by user.", vbExclamation
                Exit Sub
            End If
            
            ' Print only if the user didn't cancel
            ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
                IgnorePrintAreas:=False
        End Sub

        If that doesn't work either, please add more detailed information such as Excel version, operating system, storage medium, etc.
        If possible the file (without sensitive data).