我有一个 Word 文档,它是长聊天历史记录的导出,每个条目都包含 13 位 UNIX 时间戳。我需要将每个时间戳转换为常规/可读日期/时间(例如美国东部时间 2025 年 1 月 17 日星期五凌晨 2:47),并将常规转换附加在原始时间戳之后。 我有一个大部分有效的宏,但我的循环无法正确迭代。
该宏的目的是:
我不知道如何让 Range 变量将附加值粘贴/输入到文档中,然后迭代查找下一个时间戳(我太缺乏经验,不知道我做错了什么)
例如,第一个时间戳是“1702654591899”,预期的替换文本是:“1702654591899; Friday, December 15, 2023 - 10:36:32 AM EST”,我想重复下一个时间戳,迭代直到结束文件。
这是我带有注释的代码:
'Function converts UNIX 13 Digit Timestamp to date/time in Eastern Standard Time
'Subtracts 18,000,000 miliseconds to subtract 5 hours for Eastern Standard Time
'Adapted from FaneDuru's Solution at:
'https://stackoverflow.com/questions/73011816/excel-vba-convert-unix-timestamp-to-date-time
Function fromUNIX13DigitsEST(uT) As Date
''Original line that converts to UTC or GMT (I'm not sure which but it's
''5 hours later than intended so I commented it out but left it for info purposes)
'fromUNIX13DigitsEST = CDbl(uT) / 86400000 + DateSerial(1970, 1, 1)
'Modified from line above to account for Eastern Standard Time
fromUNIX13DigitsEST = (CDbl(uT) - 18000000) / 86400000 + DateSerial(1970, 1, 1)
End Function
'Loops and Finds all 13 digit Unix Timestamps and replaces them with the Timestamp plus
'conventional date/time
Sub FindUnix13DigitTimestampsAndAddConventionalDateAndTime()
Dim TimestampInstance As Range
Dim TimestampAndConverted As String 'Artifact of the learning process kept to prevent loops
Set TimestampInstance = ActiveDocument.Range
With TimestampInstance.Find
'Do I need more of these? Is this section the problem?
.Text = "[0-9]{13}"
.MatchWildcards = True
Do While .Execute(Forward:=True) = True
TimestampInstance.Select
' Used to test before adding the "TimestampInstance.Text = ..." line below
' Sets string variable equal to the original Timestamp and adds the standard date/time/time zone
' Uses the Function above to convert the UNIX TimeStamp
' Kept to use in the MsgBox line below as a failsafe against a runaway loop
' Adapted from FaneDuru's Solution at
' https://stackoverflow.com/questions/73011816/excel-vba-convert-unix-timestamp-to-date-time
TimestampAndConverted = TimestampInstance + "; " + Format(fromUNIX13DigitsEST(TimestampInstance), "dddd, mmmm d, yyyy - hh:nn:ss AM/PM") + " EST"
' PROBLEMATIC LINE THAT PARTIALLY DOES WHAT I INTENDED IT TO DO
' Duplicates the Function call above and adds standard date items
' Meant to replace each 13-digit timestamp with the timestamp plus standard date/time/time zone
' If commented out then loop iterates as intended and the MsgBox below displays each successive
' output from the TimestampAndConverted variable
TimestampInstance.Text = TimestampInstance + "; " + Format(fromUNIX13DigitsEST(TimestampInstance), "dddd, mmmm d, yyyy - hh:nn:ss AM/PM") + " EST"
'Displays variable TimestampAndConverted above in a message box & helps prevent a runaway loop
MsgBox TimestampAndConverted
'
'
' Should there be code here to allow the loop to iterate to next 13-digit timestamp
' or reset "TimestampInstance" variable??
'
'
''This isn't the solution since it ends up replacing the 13-digit timestamps with "" so I commented it out
'TimestampInstance.Text = ""
Loop
End With
End Sub
我设法使用字符串变量在每次迭代的 MsgBox 中显示预期的输出,但是当我尝试使用 Range 变量重复该过程并将替换文本输入到 Range 中时,它会卡在第一个时间戳上并不断重新生成在循环中添加常规日期/时间。
以第一个时间戳(“1702654591899”)为例,它随后添加的文本是“; Friday, December 15, 2023 - 10:36:32 AM EST”,但循环只是不断重新添加“; Friday, December 15” ,2023 年 - 10:36:32 AM EST”,因此最终结果为:
“1702654591899;美国东部时间 2023 年 12 月 15 日星期五 - 上午 10:36:32;美国东部时间 2023 年 12 月 15 日星期五 - 上午 10:36:32;美国东部时间 2023 年 12 月 15 日星期五 - 上午 10:36:32;星期五, 2023 年 12 月 15 日 -美国东部时间 2023 年 12 月 15 日星期五 - 上午 10:36:32;美国东部时间 2023 年 12 月 15 日星期五 - 上午 10:36:32美国东部时间上午;2023 年 12 月 15 日星期五 - 上午 10:36:32 美国东部时间;美国东部时间 2023 年 12 月 15 日星期五 - 上午 10:36:32;美国东部时间 2023 年 12 月 15 日星期五 - 上午 10:36:32; 2023 年 - 上午 10:36:32 美国东部时间...”
此时我按 CTRL+Break 循环并结束宏。
如何在每个找到的时间戳之后添加一次常规日期/时间,然后继续到下一个时间戳?
您的脚本即将完成。只需添加一行即可解决问题。
Sub FindUnix13DigitTimestampsAndAddConventionalDateAndTime()
Dim TimestampInstance As Range
Dim TimestampAndConverted As String
Set TimestampInstance = ActiveDocument.Range
With TimestampInstance.Find
.Text = "[0-9]{13}"
.MatchWildcards = True
Do While .Execute(Forward:=True) = True
' TimestampInstance.Select
TimestampAndConverted = TimestampInstance + "; " + Format(fromUNIX13DigitsEST(TimestampInstance), "dddd, mmmm d, yyyy - hh:nn:ss AM/PM") + " EST"
TimestampInstance.Text = TimestampAndConverted
' MsgBox TimestampAndConverted
TimestampInstance.Collapse Word.wdCollapseEnd ' ** change
Loop
End With
End Sub