VBA导出从Outlook到Excel的日期格式更改

时间:2013-07-02 09:46:23

标签: excel vba outlook date-format

这有点奇怪,过去几天一直在努力。

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

到目前为止,宏一直运行良好,很高兴地导出发送者,主题和日期发送和接收没有问题。

我添加了一些内容,以便我可以捕获时间和日期,如果电子邮件已被回复/转发,但这就是出错的地方。

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

我已经检查了计算机的区域设置(事实上我已经在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

1 个答案:

答案 0 :(得分:1)

这是因为你正在返回一个字符串,如果可能的话,vba将采用美国格式 - 也许使用

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