如何在Word中对字段进行条件格式设置
我有一个遵循特定名称模式的 Word 文件(doc 或 docx)。
模式是:
XXXXXXX_X_XXXXXXXX_NNNNNNN_NNNNNNN_XXXXXXXXX.doc(x)
一个例子是:
Contract_J_Smith_01032022_31032022_GOOGLE.doc(x)
我的意图是在 Word 中创建一个宏,它将获取文件名,用下划线 (_) 将其分成几部分,然后使用第四个变量创建两个变量(开始日期和结束日期)和文件名的第五部分,以便在文档中使用它们。
这些部分是格式为 ddMMyyyy 的日期。 另外,我想检查这些变量是否代表真实的日期,因此如果它们为真,则变量保持原样,如果它们为假,则警告文本将替换文档内的变量。
此警告文本应为粗体和红色。
到目前为止,我已经编写了以下代码:
Sub AutoOpen()
Dim aStory As Range
Dim aField As Field
Dim fname As String
Dim startdate As String
Dim enddate As String
fname = ActiveDocument.Name
startdate = Split(fname, "_")(3)
enddate = Split(fname, "_")(4)
startdate = Left(startdate, 2) & "/" & Mid(startdate, 3, 2) & "/" & Right(startdate, 4)
enddate = Left(enddate, 2) & "/" & Mid(enddate, 3, 2) & "/" & Right(enddate, 4)
If IsDate(startdate) = False Then
startdate = "Problem with start date in filename"
End If
If IsDate(enddate) = False Then
enddate = "Problem with end date in filename"
End If
With ActiveDocument
.Variables("startdate").Value = startdate
End With
With ActiveDocument
.Variables("enddate").Value = enddate
End With
For Each aStory In ActiveDocument.StoryRanges
For Each aField In aStory.Fields
aField.Update
Next aField
Next aStory
End Sub
唯一缺少的就是将文本设为粗体和红色。
有什么建议吗?
I have a word file (doc or docx) that follows a specific name pattern.
The pattern is:
XXXXXXX_X_XXXXXXXX_NNNNNNN_NNNNNNN_XXXXXXXXX.doc(x)
and an example is:
Contract_J_Smith_01032022_31032022_GOOGLE.doc(x)
My intention is to create a macro in Word that will get the name of the file, split it in parts by the underscore (_) and then create two variables (startdate and enddate) with the 4th and 5th parts of the filename in order to use them inside the document.
These parts are dates in the format ddMMyyyy.
Also, I want to check if these variables represent a true date, so if they are true, the variable remains as it is and if they are false, a warning text will replace the variable inside the document.
This warning text should be bold and red.
So far I have managed to write the following code:
Sub AutoOpen()
Dim aStory As Range
Dim aField As Field
Dim fname As String
Dim startdate As String
Dim enddate As String
fname = ActiveDocument.Name
startdate = Split(fname, "_")(3)
enddate = Split(fname, "_")(4)
startdate = Left(startdate, 2) & "/" & Mid(startdate, 3, 2) & "/" & Right(startdate, 4)
enddate = Left(enddate, 2) & "/" & Mid(enddate, 3, 2) & "/" & Right(enddate, 4)
If IsDate(startdate) = False Then
startdate = "Problem with start date in filename"
End If
If IsDate(enddate) = False Then
enddate = "Problem with end date in filename"
End If
With ActiveDocument
.Variables("startdate").Value = startdate
End With
With ActiveDocument
.Variables("enddate").Value = enddate
End With
For Each aStory In ActiveDocument.StoryRanges
For Each aField In aStory.Fields
aField.Update
Next aField
Next aStory
End Sub
The only things that are missing are making the text bold and red.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法是将文档中的 DOCVARIABLE 字段包装在 IF 字段中,如下所示。
以下是有关 IF 字段的文档的链接(如果您对它不熟悉。
The simplest way to do this is to wrap the DOCVARIABLE fields in your document in an IF field, as shown below.
Here is a link to documentation on the IF Field if you are not familiar with it.