在脚本中使用正确的日期和时间戳

迈克尔·查里

在我正在使用的脚本中

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之间放置一个:,但是在运行脚本时却给了我一个错误。我可以使用空格或。但是不是:这不可能吗?

皮科

在Windows中,文件名不能包含字符:这就是为什么当您尝试保存文件时出现运行时错误的原因。

您还应该使用24h格式,而不要使用AM / PM:hhmm对我而言,接缝更安全。

有关更多信息,请检查格式功能(Visual Basic for Applications)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

正确使用UTC时间戳和本地日期时间

来自分类Dev

在 Scala 中使用日期时间/时间戳

来自分类Dev

在循环中使用%time和%date时,批处理脚本中的时间戳相同

来自分类Dev

在 Java 8 中使用 replaceAll 函数获取日期和时间戳

来自分类Dev

使用时间戳和日期时间的TSQL排名

来自分类Dev

在Logstash中使用日期格式解析时间戳

来自分类Dev

在 Ecto 时间戳中使用 ISO8601 日期

来自分类Dev

使用日期和时间戳如何获取昨天。今天等

来自分类Dev

为什么我的Linq Oracle DB查询无法正确比较时间戳和日期时间?

来自分类Dev

比较日期和时间与时间戳

来自分类Dev

MySQL的日期时间和时间戳

来自分类Dev

从unix时间戳获取日期和时间

来自分类Dev

基于日期和时间戳的最大日期

来自分类Dev

使用unixtime值在时间戳上进行选择时,mysql日期时间值不正确

来自分类Dev

如何使用日期函数的时间戳参数更改日期和时间?

来自分类Dev

将日期和时间转换为日期和时间戳

来自分类Dev

尽管时间戳正确,但PHP日期显示错误的时间

来自分类Dev

从时间戳获取月份和日期

来自分类Dev

使用chron()将日期/时间戳转换为正确的R格式

来自分类Dev

PostgreSQL:如何使用时间戳仅将时间和日期插入变量?

来自分类Dev

使用Objective-C将时间戳解析为日期和时间时出现的问题

来自分类Dev

使用python将时间戳列拆分为两个单独的日期和时间列

来自分类Dev

如何使用 Laravel 5.4 比较今天的日期和日期时间戳 created_at

来自分类Dev

使用laravel和carbon正确格式化日期时间

来自分类Dev

使用正确的时区和种类解析日期时间C#

来自分类Dev

如何在Visual Studios发布生成脚本中使用生成时间戳

来自分类Dev

如何在Primefaces中使用当前时间戳获取选定日期?

来自分类Dev

在Oracle中使用时间戳比较两个日期

来自分类Dev

在R中使用时间轴(日期戳)处理图

Related 相关文章

  1. 1

    正确使用UTC时间戳和本地日期时间

  2. 2

    在 Scala 中使用日期时间/时间戳

  3. 3

    在循环中使用%time和%date时,批处理脚本中的时间戳相同

  4. 4

    在 Java 8 中使用 replaceAll 函数获取日期和时间戳

  5. 5

    使用时间戳和日期时间的TSQL排名

  6. 6

    在Logstash中使用日期格式解析时间戳

  7. 7

    在 Ecto 时间戳中使用 ISO8601 日期

  8. 8

    使用日期和时间戳如何获取昨天。今天等

  9. 9

    为什么我的Linq Oracle DB查询无法正确比较时间戳和日期时间?

  10. 10

    比较日期和时间与时间戳

  11. 11

    MySQL的日期时间和时间戳

  12. 12

    从unix时间戳获取日期和时间

  13. 13

    基于日期和时间戳的最大日期

  14. 14

    使用unixtime值在时间戳上进行选择时,mysql日期时间值不正确

  15. 15

    如何使用日期函数的时间戳参数更改日期和时间?

  16. 16

    将日期和时间转换为日期和时间戳

  17. 17

    尽管时间戳正确,但PHP日期显示错误的时间

  18. 18

    从时间戳获取月份和日期

  19. 19

    使用chron()将日期/时间戳转换为正确的R格式

  20. 20

    PostgreSQL:如何使用时间戳仅将时间和日期插入变量?

  21. 21

    使用Objective-C将时间戳解析为日期和时间时出现的问题

  22. 22

    使用python将时间戳列拆分为两个单独的日期和时间列

  23. 23

    如何使用 Laravel 5.4 比较今天的日期和日期时间戳 created_at

  24. 24

    使用laravel和carbon正确格式化日期时间

  25. 25

    使用正确的时区和种类解析日期时间C#

  26. 26

    如何在Visual Studios发布生成脚本中使用生成时间戳

  27. 27

    如何在Primefaces中使用当前时间戳获取选定日期?

  28. 28

    在Oracle中使用时间戳比较两个日期

  29. 29

    在R中使用时间轴(日期戳)处理图

热门标签

归档