【问题标题】:Can Fields.ToggleShowCodes be used on a protected Word document?Fields.ToggleShowCodes 可以用于受保护的 Word 文档吗?
【发布时间】: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 请查看代码的最新编辑。如何在不显示代码的情况下搜索交叉引用?

标签: vba ms-word


【解决方案1】:

“定位”域代码有多种可能性。我相信我在测试中复制了您的保护设置...

  1. 最接近您迄今为止使用的方法:

    ActiveWindow.View.ShowFieldCodes = True 'False 将其关闭

  2. 就我个人而言,我不喜欢在用户面前更改屏幕上的内容,除非没有其他方法可以完成任务,或者除非它对速度产生明显影响。这意味着我尽可能使用 Range 对象而不是 Selection。 Range 对象有一个属性允许您访问域代码,即使它们未显示:Range.TextRetrievalMode.IncludeFieldCodes。但是你也应该使用 Range 而不是 Selection 来进行查找。

让您开始:

Dim rng As word.Range
Set rng = ActiveDocument.content
rng.TextRetrievalMode.IncludeFieldCodes = True
With rng.Find
    'And so on, as you already have

Range.Find 和 Selection.Find 的另一个区别是后者的设置会影响 UI 中的对话框,而使用 Range.Find 不会更改对话框。

  1. 不使用 Find,而是在文档中循环 Fields 集合,检查 Field.Type,如果是 REF 字段,则应用样式。

类似的东西:

Sub LoopRefFields()
  Dim fld As word.Field

  For Each fld In ActiveDocument.Fields
    If fld.Type = wdFieldRef Then
        fld.code.Style = "Intense Reference"
    End If
  Next
End Sub

如果文档包含很多字段,您可能想要测试哪种方法最快。就个人而言,我更喜欢 (3),因为它更清楚正在发生的事情并且代码更少。

【讨论】:

  • 第一个解决方案效果很好,但由于您描述的原因,第二个和第三个更可取。不过我和他们有点麻烦。第二种方法仅在移除保护时有效(否则Replace 方法会导致错误)。使用第三个,它可以工作,但样式在PrintPreview 上恢复为正常。
  • 我已经设法通过使用Code 属性而不是Result 属性(即fld.Code.Style = "Intense Reference")使第三个解决方案工作。这使得样式更改在字段更新时保持不变。第二个解决方案没有乐趣,但第三个更优雅,所以我将它留在那里。
  • 感谢您告诉我什么对您有用。我确实测试了所有三种方法,它们都在这里工作,所以我不确定为什么你没有得到相同的结果......为 Nr 更改了代码。 3.
猜你喜欢
  • 2018-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多