【发布时间】:2016-01-09 02:59:05
【问题描述】:
我有一个启用宏的 Word 文档 (.docm),并且正在使用 Word 2007。
一个宏将“强烈引用”样式应用于所有交叉引用。
我正在尝试使用 Fields.ToggleShowCodes 在搜索参考之前显示字段。
问题在于,这仅在文档不受保护时才有效。当文档受到保护时,有没有办法做到这一点?
我有一个解决方法;我可以使用SendKeys("%{F9}") 按 ALT+F9。这有效,但它很丑陋。我真的在吹毛求疵,但我认为可能有更好的方法。
编辑:
对于一些背景知识:这是一个用于修订控制文档的模板。保护限制了可以使用的样式并锁定了文档的某些部分,例如页眉和页脚,其中包含文档属性和修订历史。这些属性是使用表单输入的(许多是自定义属性)。正文的可编辑部分被实现为适用于所有人的例外 - 这是交叉引用的地方。
编辑 2:
这是代码(减去不相关的 CharFormat 宏):
Sub InitUpdate()
'
' InitUpdate Macro - shows field codes (ALT+F9), waits 1ms (to allow for
' key presses), then calls the ExecUpdate macro.
' Used at the start of the Update Refs procedure.
'
SendKeys "%{F9}"
Application.OnTime When:=Now + 0.001, Name:="ExecUpdate"
End Sub
Sub IntenseRef()
'
' IntenseRef Macro - changes all cross references to
' 'intense reference' style (bold and blue text).
' Used in Update Refs procedure.
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles("Intense Reference")
With Selection.Find
.Text = "^19 REF"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
' Replace method causes an error if it runs before the active doc. has been
' clicked (i.e. when the file is first opened) or header/footer editing mode
' is enabled.
On Error GoTo ErrHandler
Selection.Find.Execute Replace:=wdReplaceAll
Exit Sub
ErrHandler:
If Err <> 0 Then
MsgBox ("Select anywhere in the main text first...")
Err.Clear
End If
End Sub
Sub ExecUpdate()
'
' ExecUpdate Macro - changes reference styles.
' Field codes are then hidden (ALT+F9) and the fields are updated.
' Used in Update Refs procedure (final part).
'
CharFormat
IntenseRef
SendKeys "%{F9}"
ActiveDocument.Fields.Update
End Sub
编辑 3:
在代码中添加了注释,解释了错误处理程序的必要性。
【问题讨论】:
-
为什么需要显示域代码才能进行搜索?当然可以搜索/使用域代码而不显示它们。如果您在您的问题下单击编辑并添加相关的相关代码,可能会有所帮助......
-
@CindyMeister 请查看代码的最新编辑。如何在不显示代码的情况下搜索交叉引用?