2015-04-15 73 views
0

我很放心,我終於得到了下面的代碼,在這個社區的幫助下工作。選擇PDF文件保存的地方

我在我的心願單上還有一個選項,我正在努力。目前,下面的代碼將工作表3一直保存到標題爲「發佈」的工作表中作爲單獨的PDF文件放入我選擇的文件夾中。這是由一個形狀觸發的。

我想讓下面的代碼提示文件夾選擇,以便用戶可以選擇他們的PDF文件保存在哪裏,有沒有人有任何想法如何做到這一點?

另外,呼叫殼底部將優選打開該文件的保存文件夾,但是這並不是只要真的有必要,因爲用戶知道在哪裏的文件被保存:)

Sub SaveAllPDF() 
Dim i As Integer 
Dim Fname As String 
Dim TabCount As Long 


TabCount = Sheets("Post").Index 
'Set the TabCount to the last cell you want to PDF 

' Begin the loop. 

For i = 3 To TabCount 
'Set i = the number of the first sheet you want to PDF in order from left to right To TabCount 
    If Sheets(i).Visible <> xlSheetVisible Then 
    Else 
     With Sheets(i) 
      Fname = .Range("C15") & " " & .Range("E13") & "-" & .Range("B1") 
      'The Fname above is equaling the cells that the PDF's filename will be 
      'The folder directory below is where the PDF files will be saved 
      .ExportAsFixedFormat Type:=xlTypePDF, Filename:= _ 
      "C:\Users\Brandon\Desktop\operation automated\RLtemp\" & Fname, Quality:=xlQualityStandard, _ 
      IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False 
     End With 
    End If 
Next i 

Call Shell("explorer.exe" & " " & "C:\Users\Brandon\Desktop\operation automated\RLtemp\", vbNormalFocus) 
'This opens the folder where the PDFs are saved 
End Sub 

回答

1

你可以使用Excel的FileDialog對象:

Sub SaveAllPDF() 
    Dim i As Integer 
    Dim Fname As String 
    Dim TabCount As Long 

    TabCount = Sheets("Post").index 
    'Set the TabCount to the last cell you want to PDF 

    Dim dialog As FileDialog 
    Dim path As String 

    Set dialog = Application.FileDialog(msoFileDialogFolderPicker) 
    dialog.AllowMultiSelect = False 
    If dialog.Show = -1 Then 
     path = dialog.SelectedItems(1) 
     ' Begin the loop. 
     For i = 3 To TabCount 
     'Set i = the number of the first sheet you want to PDF in order from left to right To TabCount 
      If Sheets(i).Visible <> xlSheetVisible Then 
      Else 
       With Sheets(i) 
        Fname = .Range("C15") & " " & .Range("E13") & "-" & .Range("B1") 
        'The Fname above is equaling the cells that the PDF's filename will be 
        'The folder directory below is where the PDF files will be saved 
        .ExportAsFixedFormat Type:=xlTypePDF, filename:=path & "\" & Fname, Quality:=xlQualityStandard, _ 
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False 
       End With 
      End If 
     Next i 

     Call Shell("explorer.exe" & " " & path & "\", vbNormalFocus) 
     'This opens the folder where the PDFs are saved 
    End If 
End Sub 
+0

有沒有辦法來防止彈出400錯誤消息,如果你點擊瀏覽文件夾選擇取消? – arbitel

+0

@ user3026842 - 糟糕,輕微疏忽。我*知道*我有理由檢查返回值...請參閱編輯。 – Comintern

+0

blaaah天才人。謝謝!我在一個嚴重依賴excel的辦公環境中工作,幾周前剛剛開始對VBA進行修補......如此驚人的多少你可以自動化。再次感謝! – arbitel