【问题标题】:Date format changing on VBA export from Outlook to Excel从 Outlook 到 Excel 的 VBA 导出的日期格式更改
【发布时间】:2013-07-02 09:46:22
【问题描述】:

这是最近几天一直在想的一个有点奇怪的问题。

我一直在更新 Outlook 中的一个宏,它将详细信息导出到 Excel。

到目前为止,宏运行良好,可以顺利导出发件人、主题和发送和接收的日期。

我已经添加了一点,以便在电子邮件已被回复/转发时捕获时间和日期,但这就是问题所在。

运行代码时,如果我将 Debug.Print 放在保存回复/转发日期的变量上,它会以正确的格式 (dd/mm/yyyy hh:mm:ss) 打印出来,但是当它弹出时excel 由于某种原因,它被输入为 mm/dd/yyyy hh:mm:ss(但仅适用于月份

我检查了计算机的区域设置(实际上我已经在 2 台不同的机器上尝试过),但看不到任何会导致更改的内容。

我正在使用的代码如下,有人有什么想法吗?

'this is the part that exports to Excel
intColumnCounter = intColumnCounter + 1
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = GetLastVerb(msg)
Debug.Print GetLastVerb(msg)

Public Function GetLastVerb(olkMsg As Outlook.MailItem) As String
Dim intVerb As Integer
intVerb = GetProperty(olkMsg, "http://schemas.microsoft.com/mapi/proptag/0x10810003")
Select Case intVerb
    Case 102
        Debug.Print ("Reply to Sender")
        GetLastVerb = GetLastVerbTime(olkMsg)
    Case 103
        Debug.Print ("Reply to All")
        GetLastVerb = GetLastVerbTime(olkMsg)
    Case 104
     Debug.Print ("Forward")
        GetLastVerb = olkMsg.ReceivedTime
    Case 108
     Debug.Print ("Reply to Forward")
        GetLastVerb = GetLastVerbTime(olkMsg)
    Case Else
     Debug.Print ("Unknown")
        GetLastVerb = "Not replied to"
End Select
End Function

Public Function GetProperty(olkItm As Object, strPropName As String) As Date
Dim olkPA As Outlook.PropertyAccessor
Set olkPA = olkItm.PropertyAccessor
GetProperty = olkPA.UTCToLocalTime(olkPA.GetProperty(strPropName))
Set olkPA = Nothing
End Function

Public Function GetLastVerbTime(olkItm As Object) As Variant
GetLastVerbTime = GetDateProperty(olkItm, "http://schemas.microsoft.com/mapi/proptag/0x10820040")
End Function

Public Function GetDateProperty(olkItm As Object, strPropName As String) As Date
Dim olkPA As Outlook.PropertyAccessor
Set olkPA = olkItm.PropertyAccessor
GetDateProperty = olkPA.UTCToLocalTime(olkPA.GetProperty(strPropName))
Set olkPA = Nothing
End Function

【问题讨论】:

    标签: excel vba outlook date-format


    【解决方案1】:

    这是因为您要返回一个字符串,并且如果可能,vba 将采用美国格式-也许使用

    Dim sTemp as string
    sTemp = GetLastVerb(msg)
    if isdate(stemp) then 
       rng.Value = cdate(sTemp) 
    else
       rng.value = sTemp
    end if
    

    【讨论】:

      猜你喜欢
      • 2017-07-12
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-01
      • 2018-03-21
      • 1970-01-01
      • 2019-03-13
      相关资源
      最近更新 更多