Macro de script VBA guardada en una ubicación específica

Macro de script VBA guardada en una ubicación específica

¿Cómo puedo modificar el siguiente script para que se guarde en una carpeta específica en lugar de preguntar?

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

Con gracias miguel

Respuesta1

Puede ver que el elemento seleccionado se coloca en una variable llamadaxCarpeta. Comente la sección sobre xFileDlg hasta donde se agrega xFolder.

Cambia eso a lo que quieras:

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

Respuesta2

La única razón por la que tienes que elegir una carpeta es esta parte.

Establecer xFileDlg = Application.FileDialog(msoFileDialogFolderPicker)

Si xFileDlg.Show = True Entonces xFolder = xFileDlg.SelectedItems(1) De lo contrario MsgBox "Debe especificar una carpeta para guardar el PDF". & vbCrLf & vbCrLf & "Presione OK para salir de esta macro.", vbCritical, "Debe especificar la carpeta de destino" Salir del subfin si

Esto es excelente si desea seleccionar la carpeta cada vez o necesita crear una carpeta nueva. Pero si la carpeta existe y siempre va a guardarla en el mismo lugar, simplemente elimine la parte anterior y agregue manualmente la ubicación de la carpeta.

Ejemplo:
xFolder = "C:\MyPDFs" + "\" + xSht.Name + Formato(Ahora, "yyyy-mm-dd hmmAM/PM") + ".pdf"

información relacionada