2017-03-18 34 views
0

我想编写一个例程,它从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 
+0

灰溶液应该工作。至于'ReDim'语句,您是否使用过'ReDim Preserve'来保留数组中现有的数据,然后将其调整到新的维度。 – EEM

+0

忘记包含此之前:[ReDim声明](https://msdn.microsoft.com/en-us/library/office/gg251578(v=office.15).aspx) – EEM

回答

0

唯一我认为您的ExportAsFixedFormat声明失败的原因在于您的安装中未启用选项DisplayFileAfterPublish(典型的如果您没有安装Acrobat Reader)。您可以在尝试手动导出到PDF时检查它,必须禁用选项"open file after publishing"

尝试删除参数DisplayFileAfterPublish

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\test.pdf", _ 
    Quality:=xlQualityStandard, IgnorePrintAreas:=False 

至于在帖子中的其他问题,我没有看到任何错误地把Worksheet对象的数组。但Sheets(arrSheets).Select是不合格的,并且是不必要的。它是格式不正确的,因为arrSheets参数是一个包含工作表参考的数组,而它应该是indices的数组,这就是全部。

另外我不能说为什么Redim还没有为你工作,因为你没有显示你如何使用它。

+0

数组创建循环它填充带有没有空值的工作表名称的数组。问题是ActiveSheet.ExportAsFixedFormat ...行引发“应用程序定义或对象定义的错误(运行时错误1004)”错误。有或没有DisplayFileAfterPublish arg ... Redim Preserve的作品... –

-1

这对我有用。它存在缺陷(在OP的原始版本中也存在),因为您根据表格上复选框的tabindex选择工作表编号,所选工作表可能不是您想要的工作表。你如何解决这个问题取决于你,这里超出了范围。

Private Sub CommandButton1_Click() 
    Dim intSelectionNum As Integer 
    Dim bolFound As Boolean 
    Dim ctrl As Control 

    bolFound = True  ' was a checked box found? True = NO 
    intSelectionNum = 0 ' which checkbox is it 

    For Each ctrl In FrmToPDF.Controls 
     If TypeName(ctrl) = "CheckBox" Then 
      intSelectionNum = intSelectionNum + 1 ' set the selection number 
      If ctrl.Value = True Then 
       ThisWorkbook.Sheets(intSelectionNum).Select bolFound 
       bolFound = False ' from now on we extend the selection 
      End If 
     End If 
    Next 

    If bolFound = True Then ' if there is Nothing selected send a message, or do the deal... 
     MsgBox "There is nothing selected to export!", vbOKOnly, "Nothing selected..." 
    Else 
     ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\test.pdf", Quality:=xlQualityStandard, IgnorePrintAreas:=False 
    End If 
End Sub 

这里发生了什么:我超载bolFound设置可选参数Worksheets.Select - 真正的手段取代现有的种选择,造假手段扩展当前的选择。通过启动bolFound为True,我可以清除任何现有的选择。然后将bolFound设置为False,从而扩展后续工作表的选择。我不需要数组,因为我正在让Excel为我管理选择。

注意:ThisWorkbook.Sheets(intSelectionNum).Select一次只能选择一张纸张(默认值为True),所以OP的张贴代码只会一次导出一张纸张,无论检查多少个纸盒。

我安装了Acrobat Readeer,, DisplayFileAfterPublish:=True为我工作,但我已经从示例中省略了该部分。

奖金讨论:与OP的代码一样,示例选择“表格”而不是“工作表”。这允许它导出例如图表,Excel4宏和对话框等(尽管现在只有大多数人感兴趣的图表)。如果我将“工作表”更改为“工作表”,则只会导出工作表。

强制性免责声明:这是将所选工作表导出为PDF的示例代码。它没有经过广泛的测试,并不打算成为一个直接解决方案。这个对我有用。取决于你的系统,你可能需要调整它。

编辑补充:找出选择哪些工作表(例如,建立你的数组用于其他用途),你可以在FOR/NEXT循环结束后插入此:

dim sht as Object 
For Each sht In Application.ActiveWindow.SelectedSheets 
    Debug.Print sht.Name 
Next 
+0

我试过你的代码。调试打印显示选定的工作表名称,从工作簿视图中我可以看到他们已被选中,但仍然在ActiveSheet.ExportAsFixedFormat中出现“应用程序定义的错误或对象定义的错误(运行时错误1004)”错误线。当我尝试使用数组时,这也发生了。 –

+0

...和DisplayFileAfterPublish:= True不会影响错误问题。 –