替换文档中的日期字段



我想将所有日期字段替换为,例如,"你好";。

此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

在交叉帖子中,您指定了想要的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

可以在此临时链接中找到包含此代码的文档。

关于交叉张贴礼仪,请阅读:致论坛交叉海报的信息

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

最新更新