View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.misc
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default Print a selected number of sheets

You can loop through those items in the list and build an array of all the
selected items. Then print those worksheets whose names are placed in that
array:

Option Explicit
Private Sub CancelButton_Click()
Unload Me
End Sub
Private Sub OKButton_Click()
Dim lCtr As Long
Dim sCtr As Long
Dim mySheets() As String
ReDim mySheets(1 To Me.ListBox1.ListCount)

Application.ActivePrinter = "Adobe PDF on Ne06:"

sCtr = 0
With Me.ListBox1
For lCtr = 0 To .ListCount - 1
If .Selected(lCtr) Then
sCtr = sCtr + 1
mySheets(sCtr) = .List(lCtr)
End If
Next lCtr
End With

If sCtr = 0 Then
'nothing selected
Beep
Else
Me.Hide
ReDim Preserve mySheets(1 To sCtr)
Worksheets(mySheets).PrintOut preview:=True
'Me.Show
End If

Unload Me
End Sub
Private Sub UserForm_Initialize()
With Me.ListBox1
.RowSource = ""
.AddItem Sheet3.Name
.AddItem Sheet9.Name
.AddItem Sheet2.Name
.AddItem Sheet10.Name
.MultiSelect = fmMultiSelectMulti
End With
End Sub



RobN wrote:

I have a number of sheets in a workbook and a selection of 4 need to be
printed on a regular basis. Each of those sheets have been formatted to
print a selected area.
What I would like (and I already some code, as below), is to amend the code
so that it will print to PDF but only ask for a file name once. ie it will
print the selected sheets into the one PDF document.

Rob

Option Explicit

Private Sub CancelButton_Click()
Unload Me
End Sub

Private Sub OKButton_Click()
Dim i As Long
Application.ActivePrinter = "Adobe PDF on Ne06:"

If ListBox1.Selected(0) = False And _
ListBox1.Selected(1) = False And _
ListBox1.Selected(2) = False And _
ListBox1.Selected(3) = False Then
MsgBox "Select from the list.", vbInformation
Exit Sub
End If

' choose an item
Me.Hide
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) Then
Worksheets(ListBox1.List(i)).PrintOut From:=1, To:=1
End If
Next i
'Me.Show

Dim FoundOne As Boolean

FoundOne = False
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) Then
FoundOne = True
Exit For
End If
Next i

Unload Me
End Sub

Private Sub UserForm_Initialize()
With ufPrintNo.ListBox1
.RowSource = ""
.AddItem Sheet3.Name
.AddItem Sheet9.Name
.AddItem Sheet2.Name
.AddItem Sheet10.Name
.MultiSelect = fmMultiSelectMulti
End With

End Sub


--

Dave Peterson