I am trying to code a routine which takes a user's choices of available worksheets from an Excel VBA form and exports them into one PDF document. I plan on using this with export to Word and to PowerPoint routines. I have tried several of the ideas from Stack Overflow which were said to have worked. I have had no luck. I have also tried various ideas from other sources...maybe I am blind to the obvious.
I tried using an array (arrSheets, still in the code, I have hopes that maybe I can still use it.). It populates with sheet objects, but using a dynamic array and the redim command haven't worked for me.
What I have here is code which appears to work well until it comes to the "ActiveSheet.ExportAsFixedFormat..." line. At that point I get "Application-defined or Object-defined error (Run-time error 1004)"
The following code fires from a command button on a VBA form...
Private Sub cmdExport_Click()
'Find the selected documents from the form's checkboxes and send to the export routine
Dim intArrayCounter, intSelectionNum As Integer
Dim bolFound As Boolean
Dim ctrl As control
Dim arrSheets(1 To 6) As Variant ' the array to hold the worksheet objects...
intSelectionNum = 0 ' which checkbox is it
intArrayCounter = 1 ' array index
bolFound = False ' was a checked box found?
For Each ctrl In frmToPDF.Controls
If TypeName(ctrl) = "CheckBox" Then
intSelectionNum = intSelectionNum + 1 ' set the selection number
If ctrl.Value = True Then
bolFound = True ' found a selection set the flag to true
Set arrSheets(intArrayCounter) = Sheets(intSelectionNum)
ThisWorkbook.Sheets(intSelectionNum).Select
' increment the counter
intArrayCounter = intArrayCounter + 1
End If
End If
Next
'Sheets(arrSheets).Select <---remmed out cause this throws an error
If bolFound = False Then ' if there is Nothing selected send a message, or do the deal...
Call MsgBox("There is nothing selected to export!", vbOKOnly, "Nothing selected...")
Else
frmExport.Caption = "Processing the document...Please be patient!"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\test.pdf", Quality:=xlQualityStandard, IgnorePrintAreas:=False, DisplayFileAfterPublish:=True
'the above Activesheet routine throws "Application-defined or Object-defined error (Run-time error 1004)"
End If
ThisWorkbook.Sheets(intSheet).Select
End Sub