Macro de script VBA salva em local específico

Macro de script VBA salva em local específico

Como altero o script a seguir para que ele seja salvo em uma pasta específica em vez de perguntar?

Sub Saveaspdfandsend()
Dim xSht As Worksheet
Dim xFileDlg As FileDialog
Dim xFolder As String
Dim xYesorNo As Integer
Dim xOutlookObj As Object
Dim xEmailObj As Object
Dim xUsedRng As Range

Set xSht = ActiveSheet
Set xFileDlg = Application.FileDialog(msoFileDialogFolderPicker)

If xFileDlg.Show = True Then
   xFolder = xFileDlg.SelectedItems(1)
Else
   MsgBox "You must specify a folder to save the PDF into." & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Must Specify Destination Folder"
   Exit Sub
End If
xFolder = xFolder + "\" + xSht.Name + Format(Now, " yyyy-mm-dd hmmAM/PM") + ".pdf"

'Check if file already exist
If Len(Dir(xFolder)) > 0 Then
    xYesorNo = MsgBox(xFolder & " already exists." & vbCrLf & vbCrLf & "Do you want to overwrite it?", _
                      vbYesNo + vbQuestion, "File Exists")
    On Error Resume Next
    If xYesorNo = vbYes Then
        Kill xFolder
    Else
        MsgBox "if you don't overwrite the existing PDF, I can't continue." _
                    & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Exiting Macro"
        Exit Sub
    End If
    If Err.Number <> 0 Then
        MsgBox "Unable to delete existing file.  Please make sure the file is not open or write protected." _
                    & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Unable to Delete File"
        Exit Sub
    End If
End If

Set xUsedRng = xSht.UsedRange
If Application.WorksheetFunction.CountA(xUsedRng.Cells) <> 0 Then
    'Save as PDF file
    xSht.ExportAsFixedFormat Type:=xlTypePDF, Filename:=xFolder, Quality:=xlQualityStandard

    'Create Outlook email
    Set xOutlookObj = CreateObject("Outlook.Application")
    Set xEmailObj = xOutlookObj.CreateItem(0)
    With xEmailObj
        .Display
        .To = ""
        .CC = "[email protected]"
        .Subject = xSht.Name + ".pdf"
        .Attachments.Add xFolder
        If DisplayEmail = False Then
            '.Send
        End If
    End With
Else
  MsgBox "The active worksheet cannot be blank"
  Exit Sub
End If
End Sub

Com agradecimentos, Miguel

Responder1

Você pode ver que o item selecionado está sendo colocado em uma variável chamadaxPasta. Comente a seção sobre xFileDlg até onde xFolder está anexado.

Mude para o que quiser:

xFolder = xFolder + "\" + xSht.Name + Format(Now, " yyyy-mm-dd hmmAM/PM") + ".pdf"

Responder2

A única razão pela qual você precisa escolher uma pasta é esta parte.

Definir xFileDlg = Application.FileDialog(msoFileDialogFolderPicker)

Se xFileDlg.Show = True Então xFolder = xFileDlg.SelectedItems(1) Else MsgBox "Você deve especificar uma pasta para salvar o PDF." & vbCrLf & vbCrLf & "Pressione OK para sair desta macro.", vbCritical, "Deve especificar a pasta de destino" Exit Sub End If

Isso é ótimo se você quiser escolher a pasta sempre ou precisar criar uma nova pasta. Mas se a pasta existir e você sempre for salvá-la no mesmo lugar, basta retirar a parte acima e adicionar manualmente o local da pasta.

Exemplo:
xFolder = "C:\MyPDFs" + "\" + xSht.Name + Format(Agora, "aaaa-mm-dd hmmAM/PM") + ".pdf"

informação relacionada