+ Reply to Thread
Results 1 to 6 of 6

Print Dialogue Box

Hybrid View

  1. #1
    Registered User
    Join Date
    03-25-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    44

    Print Dialogue Box

    Hi, I came accross this really useful little routine to select specific sheets to print. The advantage for me of this routine is that each sheet is sent as its own print job abd not as a batch in a single job, therefore my page settings are maintained. The only thing I am struggling to so is add a select all check box, so that i can select all visible sheets and de-select the odd one or two. Can you have a look at the code and let me know if this can be acheived.

    Many thanks in advance.

    Code is:-

    Sub SelectSheets ()
    Dim i As Integer
        Dim TopPos As Integer
        Dim SheetCount As Integer
        Dim PrintDlg As DialogSheet
        Dim CurrentSheet As Worksheet
        Dim cb As CheckBox
        Application.ScreenUpdating = False
    
    '   Check for protected workbook
        If ActiveWorkbook.ProtectStructure Then
            MsgBox "Workbook is protected.", vbCritical
            Exit Sub
        End If
    
    '   Add a temporary dialog sheet
        Set CurrentSheet = ActiveSheet
        Set PrintDlg = ActiveWorkbook.DialogSheets.Add
    
        SheetCount = 0
    
    '   Add the checkboxes
    
        TopPos = 40
        For i = 1 To ActiveWorkbook.Worksheets.Count
            Set CurrentSheet = ActiveWorkbook.Worksheets(i)
    '       Skip empty sheets and hidden sheets
            If Application.CountA(CurrentSheet.Cells) <> 0 And _
                CurrentSheet.Visible Then
                SheetCount = SheetCount + 1
                PrintDlg.CheckBoxes.Add 78, TopPos, 150, 16.5
                    PrintDlg.CheckBoxes(SheetCount).Text = _
                        CurrentSheet.Name
                TopPos = TopPos + 13
            End If
        Next i
    
    '   Move the OK and Cancel buttons
        PrintDlg.Buttons.Left = 240
    
    '   Set dialog height, width, and caption
        With PrintDlg.DialogFrame
            .Height = Application.Max _
                (68, PrintDlg.DialogFrame.Top + TopPos - 34)
            .Width = 230
            .Caption = "Select sheets to print"
    
        End With
    
    '   Change tab order of OK and Cancel buttons
    '   so the 1st option button will have the focus
        PrintDlg.Buttons("Button 2").BringToFront
        PrintDlg.Buttons("Button 3").BringToFront
    
    '   Display the dialog box
        CurrentSheet.Activate
        Application.ScreenUpdating = True
        If SheetCount <> 0 Then
            If PrintDlg.Show Then
                For Each cb In PrintDlg.CheckBoxes
                    If cb.Value = xlOn Then
                        Worksheets(cb.Caption).Activate
                        ActiveSheet.PrintOut
    '                   ActiveSheet.PrintPreview 'for debugging
    
                    End If
                Next cb
            End If
        Else
            MsgBox "All worksheets are empty."
        End If
    
    '   Delete temporary dialog sheet (without a warning)
        Application.DisplayAlerts = False
        PrintDlg.Delete
    
    '   Reactivate original sheet
        CurrentSheet.Activate
    End Sub
    Last edited by Macca_GB; 05-21-2014 at 08:38 AM. Reason: Added code tags

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,978

    Re: Print Dialogue Box

    Rather than using the somewhat antiquated dialogsheets, I would suggest you use a userform with a multiselect listbox on it. That way all your controls can be added and coded at design time and all that happens at run-time is to populate the list with the sheet names.
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Registered User
    Join Date
    03-25-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    44

    Re: Print Dialogue Box

    Thanks, no clue how to do that, so will research.

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,978

    Re: Print Dialogue Box

    Here's a simple example. The listbox has had its MultiSelect property set.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    03-25-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    44

    Re: Print Dialogue Box

    Rory, worked a treat and taught me loads abour user forms, thanks very much, appreciated.

  6. #6
    Registered User
    Join Date
    03-25-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    44

    Re: Print Dialogue Box

    Hi, apologies for the duplication but I could not see my original post listed so thought it hadn't been submitted. As I now have a infraction point does that mean my query will wait?

    Thanks

+ 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. Open Print Dialogue box
    By peely-peel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-04-2008, 07:25 PM
  2. code for print dialogue
    By BorisS in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-18-2006, 07:25 AM
  3. Suppressing print dialogue box
    By sgrech in forum Excel General
    Replies: 0
    Last Post: 09-24-2005, 07:14 AM
  4. Closing the Print Dialogue box
    By vvaidya@pol.net in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-07-2005, 05:05 PM
  5. [SOLVED] how can I add a print dialogue?
    By Rob in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-05-2005, 07:06 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