Verwenden des richtigen Datums- und Zeitstempels im Skript

Verwenden des richtigen Datums- und Zeitstempels im Skript

In dem von mir verwendeten Skript habe ich

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

das zeigt 2018 04 06 245PM. Ich habe versucht, ein : zwischen h und mm einzufügen, aber beim Ausführen des Skripts erhalte ich einen Fehler. Ich kann ein Leerzeichen oder ein . verwenden, aber kein :. Ist das nicht möglich?

Antwort1

Unter Windows dürfen Dateinamen das Zeichen nicht enthalten :. Aus diesem Grund tritt beim Versuch, die Datei zu speichern, ein Laufzeitfehler auf.

Sie sollten auch ein 24-Stunden-Format anstelle von AM/PM verwenden: hhmmdas erscheint mir sicherer.

ÜberprüfenFormatfunktion (Visual Basic für Anwendungen)für mehr Informationen.

Antwort2

Für einen korrekten Datums- und Zeitstempel sollte Ihre VB-Befehlszeile lauten:

Format(now(), "yyyy-MM-dd hh:mm:ss")

oder

Format(DateTime.Now, "yyyy-MM-dd hh:mm:ss")

Hinweis: DateTime.NowGibt einen Wert vom Datentyp „Datum“ zurück. Datumsvariablen zeigen Daten entsprechend dem auf Ihrem System eingestellten kurzen Datums- und Zeitformat an.

Um AM/PM anzuzeigen, ändern Sie Ihre Formatzeichenfolge wie folgt:

Format(TimeValue(Now), 'hh:mm AM/PM')

verwandte Informationen