2017-03-18 35 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不會影響錯誤問題。 –