我想编写一个例程,它从Excel VBA表单中获取可用工作表的用户选择并将它们导出到一个PDF文档中。我计划使用此功能导出到Word和PowerPoint例程。我已经尝试了Stack Overflow的几个被认为有效的想法。我没有运气。我也尝试了其他来源的各种想法......也许我对明显的盲目了。我试过使用一个数组(arrSheets,仍然在代码中,我希望也许我仍然可以使用它)。它使用表单对象填充,但使用动态数组和redim命令对我没有效果。激活多个工作表使用VBA导出
我在这里得到的代码似乎很好地工作,直到涉及到“ActiveSheet.ExportAsFixedFormat ...”行。在这一点上,我得到“应用程序定义或对象定义的错误(运行时错误1004)”
从VBA窗体上的命令按钮下面的代码火灾...
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
灰溶液应该工作。至于'ReDim'语句,您是否使用过'ReDim Preserve'来保留数组中现有的数据,然后将其调整到新的维度。 – EEM
忘记包含此之前:[ReDim声明](https://msdn.microsoft.com/en-us/library/office/gg251578(v=office.15).aspx) – EEM