스크립트에서 적절한 날짜 및 시간 스탬프 사용

스크립트에서 적절한 날짜 및 시간 스탬프 사용

내가 사용하는 스크립트에는

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

2018 04 06 245PM이 표시됩니다. h와 mm 사이에 :를 넣으려고 했지만 스크립트를 실행할 때 오류가 발생합니다. 공백이나 . 하지만 a는 아닙니다. 이것이 불가능합니까?

답변1

Windows에서는 파일 이름에 문자를 포함할 수 없습니다 :. 이것이 파일을 저장하려고 할 때 런타임 오류가 발생하는 이유입니다.

또한 AM/PM 대신 24시간 형식을 사용해야 합니다. hhmm이음새가 더 안전합니다.

확인하다형식 함수(응용 프로그램용 Visual Basic)자세한 내용은.

답변2

적절한 날짜 및 시간 스탬프를 얻으려면 VB 명령줄이 다음과 같아야 합니다.

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

또는

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

참고: DateTime.Now날짜 데이터 유형의 값을 반환합니다. 날짜 변수는 시스템에 설정된 간단한 날짜 및 시간 형식에 따라 날짜를 표시합니다.

AM/PM을 표시하려면 형식 문자열을 다음과 같이 변경하세요.

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

관련 정보