1

我有一个word文档。它包含我需要修改的时间戳。

我正在使用 VBA 来修改时间戳。我能够找到时间戳。我能够提取时间戳并调整时间戳中提取的时间。我无法替换旧的时间戳。

问题:我需要什么 VBA 来替换旧的时间戳?

应该不难,但我不知道 vba 或 word lingo 来做谷歌搜索。

我正在使用 Word 文档。我将提供一些纯文本以避免原始 word 文档出现问题。示例文本:https ://pastebin.com/raw/nf4Cc8bu

这是我的代码。

    Sub FindAndReplace()
'
' vba string functions
'    https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/functions/string-functions
'    https://www.eng.auburn.edu/~tplacek/courses/3600/Strings%20and%20Manipulations.htm
'
    Dim c As Range
    Dim StartWord As String, EndWord As String
    Debug.Print
    Debug.Print "start of FindAndReplace() at " & Time() & " on " & Date
    Debug.Print "ActiveWindow.Parent (word document name) is " & ActiveWindow.Parent
    Debug.Print

    Set c = ActiveDocument.Content
    c.Find.ClearFormatting
    c.Find.Replacement.ClearFormatting
   
    With c.Find
        .Text = "[0-9][0-9]:[0-9][0-9]:[0-9][0-9]"    ' Time stamp format
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = True
        .MatchSoundsLike = False
        .MatchAllWordForms = False '
    End With
     
    Dim FirstDate As Date    ' Declare variables.
    Dim IntervalType As String
    Dim Number As Integer
    Dim message As String
    ' For how much time do we need to adjust the timestamp so to match the video?
    'Number = InputBox("Enter number of seconds")
    Number = 45
    Debug.Print "Number is " & Number
    Debug.Print
    IntervalType = "s"    '  interval in seconds
    c.Find.Execute
    While c.Find.Found
        Debug.Print "c.Text is " & c.Text
        message = DateAdd(IntervalType, Number, c.Text)
        Debug.Print "adjusted time stamp is " & Format(message, "hh:mm:ss")
'--> need to replace existing timestamp here.
        Debug.Print
        ' get next timestamp
        c.Find.Execute
    Wend
    
   Debug.Print "Good bye..."

End Sub
4

0 回答 0