VBA-Skriptmakro an einem bestimmten Speicherort speichern

VBA-Skriptmakro an einem bestimmten Speicherort speichern

Wie ändere ich das folgende Skript, sodass es in einem bestimmten Ordner speichert, ohne danach zu fragen?

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

Mit Dank, Michael

Antwort1

Sie können sehen, dass das ausgewählte Element in eine Variable namens eingefügt wirdxOrdner. Kommentieren Sie den Abschnitt über xFileDlg bis zu der Stelle aus, an die xFolder angehängt wird.

Ändern Sie es nach Belieben:

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

Antwort2

Der einzige Grund, warum Sie einen Ordner auswählen müssen, ist dieser Teil.

Setzen Sie xFileDlg = Application.FileDialog(msoFileDialogFolderPicker).

Wenn xFileDlg.Show = True, dann xFolder = xFileDlg.SelectedItems(1), sonst MsgBox „Sie müssen einen Ordner angeben, in dem die PDF-Datei gespeichert werden soll.“ & vbCrLf & vbCrLf & „Drücken Sie OK, um dieses Makro zu beenden.“, vbCritical, „Zielordner muss angegeben werden“ Exit Sub End If

Dies ist ideal, wenn Sie den Ordner jedes Mal auswählen möchten oder einen neuen Ordner erstellen müssen. Wenn der Ordner jedoch vorhanden ist und Sie ihn immer am selben Ort speichern möchten, entfernen Sie einfach den obigen Teil und fügen Sie den Ordnerspeicherort manuell hinzu.

Beispiel:
xFolder = "C:\MyPDFs" + "\" + xSht.Name + Format(Jetzt, " yyyy-mm-dd hmmAM/PM") + ".pdf"

verwandte Informationen