替换文档中的日期字段

Replace datefields in a document

我想用例如“你好”替换所有日期字段。

此 Word VBA 代码替换了文档页眉和页脚中的所有字段。我只想替换日期字段。

Sub test()
    Dim oField As Field
    Dim oSection As Section
    Dim oHeader As HeaderFooter
    Dim oFooter As HeaderFooter
    
    For Each oSection In ActiveDocument.Sections
    
        For Each oHeader In oSection.Headers
            If oHeader.Exists Then
                For Each oField In oHeader.Range.Fields
                    
                    If oField = wdFieldDate Then
                        oField.Result.Text = "hello"
                        oField.Unlink
                    Else
                    End If
                    
                Next oField
            End If
        Next oHeader
    
        For Each oFooter In oSection.Footers
            If oFooter.Exists = True Then
                 For Each oField In oFooter.Range.Fields
                     
                     If IsDate(oField) = True Then
                        oField.Result.Text = "hello"
                        oField.Unlink
                    Else
                    End If
                    
                Next oField
            End If
        Next oFooter
    
    Next oSection
End Sub

If oField = wdFieldDate Then 无法编译,因为您尚未指定要检查的字段 属性。您的代码应如下所示

Sub test()
   Dim oField As Field
   Dim oSection As Section
   Dim oHeader As HeaderFooter
   Dim oFooter As HeaderFooter

   For Each oSection In ActiveDocument.Sections
      For Each oHeader In oSection.Headers
         If oHeader.Exists Then
            For Each oField In oHeader.Range.Fields
               'check the field type
               If oField.Type = wdFieldDate Then
                  oField.Result.Text = "hello"
                  oField.Unlink
               Else
               End If
            Next oField
         End If
      Next oHeader

      For Each oFooter In oSection.Footers
         If oFooter.Exists = True Then
            For Each oField In oFooter.Range.Fields
               If oField.Type = wdFieldDate Then
                  oField.Result.Text = "hello"
                  oField.Unlink
               Else
               End If
            Next oField
         End If
      Next oFooter
   Next oSection
End Sub

在您的 cross-post 中,您指定了需要 DATE 和 TIME 字段。

Sub DateFieldsReplace()
    ' Replace any date fields in active document
    ' Charles Kenyon  2020-09-09

    ' https://answers.microsoft.com/de-de/msoffice/forum/all/word-macro-search-for-date-fields-and-replace/ad578c92-e1ce-4258-903f-552dfae2a843
    ' =====================================================
    ' DECLARE VARIABLES AND CONSTANTS
    Dim oField As Field, bErrMark As Boolean, strPrompt As String, bFieldCodeHidden As Boolean
    Dim oStory As Range
    Const strREPLACETEXT = "Hello" ' Change to suit
        '
    ' =====================================================
    ' TURN OFF SCREEN UPDATING
    ' Application.ScreenUpdating = False
    On Error GoTo OOPS
    Let bFieldCodeHidden = ActiveWindow.View.ShowFieldCodes ' get current setting for field code display
    Let ActiveWindow.View.ShowFieldCodes = True
    '
    ' =====================================================
    ' FIND AND REPLACE DATE FIELDS
    For Each oStory In ActiveDocument.StoryRanges
        With oStory.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = "^d Date"
            .Replacement.Text = strREPLACETEXT
            .Execute Replace:=wdReplaceAll
            .Text = "^d Time"
            .Execute Replace:=wdReplaceAll
        End With
    Next oStory
    '
    Let strPrompt = "All Date fields replaced with " & strREPLACETEXT
    GoTo ResumeMacro
    ' =====================================================
    ' ERROR HANDLER
OOPS:
    Let strPrompt = "Sorry. There was a problem with the macro DateFieldsReplace."
    '
ResumeMacro:
    '
    ' =====================================================
    ' RETURN SCREEN UPDATING AND FINISH
    With ActiveDocument.Range.Find
        .ClearFormatting
        .Text = ""
        .Replacement.ClearFormatting
        .Replacement.Text = ""
    End With
    Application.ScreenUpdating = True
    Application.ScreenRefresh
    Set oField = Nothing

    Set oStory = Nothing
    Let ActiveWindow.View.ShowFieldCodes = bFieldCodeHidden
    On Error GoTo -1
    
    MsgBox strPrompt
    '
End Sub

可以在 this temporary link.

找到包含此代码的文档

cross-posting礼仪请阅读:A Message to Forum Cross-Posters