You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Word VBA宏问题:为13位UNIX时间戳追加可读时间并循环处理

问题描述

我有一份导出的长聊天记录Word文档,每条记录包含一个13位UNIX时间戳。需要将每个时间戳转换为可读的EST格式日期时间(如Friday, January 17, 2025 2:47 AM EST)并追加在原时间戳后。

预期实现功能:

  • 搜索所有13位时间戳
  • 将时间戳转换为可读日期时间
  • 将转换结果追加到原时间戳后
  • 迭代处理下一个时间戳

但当前宏在修改第一个时间戳后,会重复追加转换结果,无法定位到下一个时间戳。现有VBA代码如下:

'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

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

      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
问题原因

修改TimestampInstance.Text后,查找范围没有更新,导致每次循环都会重新匹配刚修改内容里的原始时间戳(原时间戳仍在文本开头),陷入重复处理同一个位置的死循环。

修正后的代码
'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
Function fromUNIX13DigitsEST(uT) As Date
   'Modified 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 appends the converted date/time
Sub FindUnix13DigitTimestampsAndAddConventionalDateAndTime()
    Dim TimestampInstance As Range
    Dim originalTimestamp As String
    Dim convertedDateTime As String
    
    Set TimestampInstance = ActiveDocument.Range
    
    With TimestampInstance.Find
        .Text = "[0-9]{13}"
        .MatchWildcards = True
        .Wrap = wdFindStop ' 避免查找至文档末尾后从头循环
        
        Do While .Execute(Forward:=True) = True
            ' 先保存原始时间戳,防止修改Range后丢失正确值
            originalTimestamp = TimestampInstance.Text
            ' 生成符合要求的EST格式日期时间
            convertedDateTime = "; " & Format(fromUNIX13DigitsEST(originalTimestamp), "dddd, mmmm d, yyyy hh:mm AM/PM") & " EST"
            
            ' 更新文本:原时间戳 + 转换后的日期时间
            TimestampInstance.Text = originalTimestamp & convertedDateTime
            
            ' 重置查找范围,从当前位置的下一个字符开始,避免重复匹配
            Set TimestampInstance = ActiveDocument.Range(TimestampInstance.End, ActiveDocument.Content.End)
        Loop
    End With
End Sub
关键修改说明
  • 新增originalTimestamp变量保存原始时间戳,避免修改Range后无法获取正确的原始值
  • 设置.Wrap = wdFindStop,防止查找至文档末尾后从头开始循环
  • 每次处理完一个时间戳后,重新设置查找范围为当前位置到文档末尾,确保下一次查找从已处理内容之后开始,彻底解决重复匹配问题
  • 调整格式字符串,移除多余的分隔符,匹配示例中的日期格式

内容的提问来源于stack exchange,提问作者GuarPhad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 21:54:59