我有一个 Word 文档,它是长聊天记录的导出,每个条目都包含一个 13 位 UNIX 时间戳。我需要将每个时间戳转换为常规/可读的日期/时间(例如,2025 年 1 月 17 日星期五,美国东部时间凌晨 2:47),并将常规转换附加在原始时间戳之后。我有一个宏,它基本上可以正常工作,但我的循环无法正确迭代。
该宏的目的是:
- 搜索每个 13 位时间戳
- 将该时间戳转换为常规日期/时间
- 在时间戳后添加常规日期/时间
- 迭代并找到下一个时间戳并重复
我不知道如何让 Range 变量将附加值粘贴/输入到文档中,然后迭代查找下一个时间戳(我太缺乏经验,不知道我做错了什么)
例如,第一个时间戳是“1702654591899”,预期的替换文本是:“1702654591899;2023 年 12 月 15 日星期五 - 美国东部标准时间上午 10:36:32”,我想重复下一个时间戳,直至文档结束。
以下是我带有注释的代码:
'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”)为例,它随后添加的文本是“;2023 年 12 月 15 日星期五 - 美国东部标准时间上午 10:36:32”,但循环只是不断重新添加“;2023 年 12 月 15 日星期五 - 美国东部标准时间上午 10:36:32”,所以最终结果是:
“1702654591899;2023 年 12 月 15 日星期五 - 上午 10:36:32 EST;2023 年 12 月 15 日星期五 - 上午 10:36:32 EST;2023 年 12 月 15 日星期五 - 上午 10:36:32 EST;2023 年 12 月 15 日星期五 - 上午 10:36:32 EST;2023 年 12 月 15 日星期五 - 上午 10:36:32 EST;2023 年 12 月 15 日星期五 - 上午 10:36:32 EST;2023 年 12 月 15 日星期五 - 上午 10:36:32 EST;2023 年 12 月 15 日星期五 - 上午 10:36:32美国东部标准时间;2023 年 12 月 15 日星期五 - 上午 10:36:32 美国东部标准时间;2023 年 12 月 15 日星期五 - 上午 10:36:32 美国东部标准时间;2023 年 12 月 15 日星期五 - 上午 10:36:32 美国东部标准时间……”
此时我按 CTRL+Break 键终止循环并结束宏。
如何在每个找到的时间戳后添加一次常规日期/时间,然后继续下一个时间戳?
您的脚本已接近完成。只需添加一行即可解决问题。