2013-11-21 24 views
0

我有一個頁面的excel文件,該文件根據下拉選項進行更改。我需要能夠將每個數據集導出到一個PDF中。所以,我正在尋找一個宏,它會循環顯示下拉菜單中的每個選項,並將每個數據集保存爲多頁PDF文件。將同一個Excel頁面的多個版本複製到一個PDF中

我的想法是創建循環並將每個版本保存爲臨時工作表。然後我可以使用

ThisWorkbook.Sheets(Array("Sheet1", "Sheet2")).Select 

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _ 
    "C:\tempo.pdf", Quality:= xlQualityStandard, IncludeDocProperties:=True, _ 
    IgnorePrintAreas:=False, OpenAfterPublish:=True 

將所有工作表保存爲一個PDF,但然後我需要刪除所有的臨時文件。

謝謝, 克里斯

+2

刪除臨時添加表似乎並不像一個大下側。如果您創建了一個新工作簿來放置工作表,那麼您可以在不保存的情況下關閉工作表,然後您就完成了... –

回答

0

這裏是我的解決辦法:

Sub LoopThroughDD() 

'Created by Chrismas007 

Dim DDLCount As Long 
    Dim TotalDDL As Long 
    Dim CurrentStr As String 
    TotalDDL = Sheets("Report").DropDowns("Drop Down 10").ListCount 

'Loops through DropDown stores 
    For DDLCount = 1 To TotalDDL 
     Sheets("Report").DropDowns("Drop Down 10").Value = DDLCount 
    CurrentStr = "Report" & DDLCount 
'Creates a copy of each store and pastes them in a new worksheet 
    Sheets.Add(After:=Sheets(Worksheets.Count)).Name = "Report" & DDLCount 
    Sheets("Report").Columns("D:V").Copy 
    Sheets(CurrentStr).Columns("A:S").Insert Shift:=xlToRight 
    Sheets(CurrentStr).Range("A1:S98").Select 
    Selection.Copy 
    Sheets(CurrentStr).Range("A1:S98").Select 
    Selection.PasteSpecial Paste:=xlPasteValues, _ 
    Operation:=xlNone, SkipBlanks:=False, Transpose:=False 
    Application.CutCopyMode = False 
    Sheets(CurrentStr).PageSetup.PrintArea = "$A$1:$S$98" 
'Sets worksheet to one page 
    With Sheets(CurrentStr).PageSetup 
     .LeftMargin = Application.InchesToPoints(0.5) 
     .RightMargin = Application.InchesToPoints(0.5) 
     .TopMargin = Application.InchesToPoints(0.5) 
     .BottomMargin = Application.InchesToPoints(0.5) 
     .HeaderMargin = Application.InchesToPoints(0) 
     .FooterMargin = Application.InchesToPoints(0) 
     .FitToPagesWide = 1 
     .FitToPagesTall = 1 
     .Zoom = False 
     .CenterHorizontally = True 
     .CenterVertically = True 
     End With 
    Next DDLCount 
'Because only visable worksheets will be captured on PDF dump, need to hide temporarily 
    Sheets("Report").Visible = False 

    Dim TheOS As String 
    Dim dd As DropDown 

'Going to name the file as the rep name so grabbing that info here 
    Set dd = Sheets("Report").DropDowns("Drop Down 2") 

    TheOS = Application.OperatingSystem 

'Select all visible worksheets and export to PDF 
    Dim ws As Worksheet 
     For Each ws In Sheets 
     If ws.Visible Then ws.Select (False) 
    Next 

    If InStr(1, TheOS, "Windows") > 0 Then 
     ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _ 
        ThisWorkbook.Path & "\" & dd.List(dd.ListIndex), Quality:=xlQualityStandard, _ 
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _ 
        False 

    Else 
     ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _ 
        ThisWorkbook.Path & ":" & dd.List(dd.ListIndex), Quality:=xlQualityStandard, _ 
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _ 
        False 
     End If 

'Unhide our original worksheet 
    Sheets("Report").Visible = True 

    TotalDDL = Sheets("Report").DropDowns("Drop Down 10").ListCount 

'Delete all temp worksheets 
    For DDLCount = 1 To TotalDDL 
     CurrentStr = "Report" & DDLCount 
     Application.DisplayAlerts = False 
     Sheets(CurrentStr).Delete 
     Application.DisplayAlerts = True 
    Next DDLCount 



    DDLCount = Empty 
End Sub 
1

我建議單獨導出他們所有PDF到一個臨時目錄,將它們訂在一起使用Adobe的COM自動化庫(假設你有專業版),然後刪除臨時文件夾。

Public Sub JoinPDF_Folder(ByVal strFolderPath As String, ByVal strOutputFileName As String) 
On Error GoTo ErrHandler: 

    Dim AcroExchPDDoc As Object, _ 
     AcroExchInsertPDDoc As Object 
    Dim strFileName As String 
    Dim iNumberOfPagesToInsert As Integer, _ 
     iLastPage As Integer 
    Set AcroExchPDDoc = CreateObject("AcroExch.PDDoc") 

    Dim strFirstPDF As String 

' Get the first pdf file in the directory 
    strFileName = Dir(strFolderPath + "*.pdf", vbNormal) 
    strFirstPDF = strFileName 

' Open the first file in the directory 
    If Not (AcroExchPDDoc.Open(strFolderPath & strFileName)) Then 
     Err.Raise 55555, "JoinPDF_Folder", "Could not open PDF for joining" 
    End If 

' Get the name of the next file in the directory [if any] 
    If strFileName <> "" Then 
     strFileName = Dir 

    ' Start the loop. 
     Do While strFileName <> "" 

    ' Get the total pages less one for the last page num [zero based] 
      iLastPage = AcroExchPDDoc.GetNumPages - 1 
      Set AcroExchInsertPDDoc = CreateObject("AcroExch.PDDoc") 

     ' Open the file to insert 
      If Not (AcroExchInsertPDDoc.Open(strFolderPath & strFileName)) Then 
       Err.Raise 55555, "JoinPDF_Folder", "Could not open PDF for joining" 
      End If 

     ' Get the number of pages to insert 
      iNumberOfPagesToInsert = AcroExchInsertPDDoc.GetNumPages 

     ' Insert the pages 
      AcroExchPDDoc.InsertPages iLastPage, AcroExchInsertPDDoc, 0, iNumberOfPagesToInsert, True 

     ' Close the document 
      AcroExchInsertPDDoc.Close 

     ' Delete the document 
      Kill strFolderPath & strFileName 

     ' Get the name of the next file in the directory 
      strFileName = Dir 
     Loop 

    ' Save the entire document as the strOutputFileName using SaveFull [0x0001 = &H1] 
     If Not (AcroExchPDDoc.Save(PDSaveFull, strOutputFileName)) Then 
      Err.Raise 55556, "JoinPDF_Folder", "Could not save joined PDF" 
     End If 
    End If 

    ' Close the PDDoc 
    AcroExchPDDoc.Close 

    Kill strFolderPath & strFirstPDF 
    CallStack.Pop 
    Exit Sub 

ErrHandler: 
    GlobalErrHandler 
End Sub 
+0

我有PRO,但是這對於大約20或30位用戶可用,並非所有用戶都有臨。 – user3019631

+0

@ user3019631他們是否安裝了Adobe PDF打印機驅動程序?他們可以選擇打印到「Adobe PDF」作爲打印機嗎?您可以通過拉起notepad.exe並選擇「打印...」來測試這一點。查看「Adobe PDF」是否是可用的打印機之一。 – Blackhawk

相關問題