2015-04-15 6 views
0

Я так рад, что наконец-то получил код ниже, чтобы работать с помощью этого сообщества.Выбор Где PDF-файлы Сохранить

У меня есть еще один вариант в моем списке пожеланий, с которым я борюсь. В настоящее время приведенный ниже код сохранит рабочий лист 3 до рабочего листа под названием «post» в виде отдельных PDF-файлов в папку, которую я выбираю. Это вызвано формой.

Я пытаюсь сделать приведенный ниже код приглашением папки, чтобы пользователи могли выбирать, где сохраняются файлы PDF, есть ли у кого-нибудь идеи, как это сделать?

Кроме того, вызов Shell в нижней бы предпочтительно открыть папку, в которой сохранены файлы, но это на самом деле не нужно до тех пор, как пользователи знают, где файлы сохраняются :)

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

You можно просто использовать в 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 ошибке, которое появляется, если вы нажмете отмена на выбор папки Browse? – arbitel

+0

@ user3026842 - К сожалению, небольшой недосмотр. Я * знал * был я причина, я проверял возвращаемое значение ... См. Редактирование. – Comintern

+0

blaaah гений человек. Спасибо! Я работаю в офисной среде, которая в значительной степени опирается на excel и только что начала заниматься VBA несколько недель назад .. настолько удивительно, насколько вы можете автоматизировать. снова спасибо! – arbitel

Смежные вопросы