无法关闭 Word VBA 中的设计模式

发布于 2024-09-09 04:52:08 字数 491 浏览 2 评论 0原文

我对这件事感到困惑 - 在录制宏时,我基本上添加了一些字段并进入设计模式以便能够替换占位符的虚拟文本。现在,我在录制宏时退出设计模式,一切似乎都工作正常。但在播放宏时,它会在 ActiveDocument.ToggleFormsDesign 之后停止。

可能是什么原因造成的?还有其他人经历过吗?

这是宏的片段:

Selection.Range.ContentControls.Add (wdContentControlText)
ActiveDocument.ToggleFormsDesign
Selection.TypeText Text:="Date"
Selection.MoveLeft Unit:=wdCharacter, Count:=4, Extend:=wdExtend
Selection.Style = ActiveDocument.Styles("TextRed")
ActiveDocument.ToggleFormsDesign

I'm stumped about this thing - when recording a macro where I basically add some fields and go into Design Mode to be able to replace the dummy text of the placeholder. Now, I go out of Design Mode when recording the macro and everything seems to be working ok. But when playing the macro, it just stops after ActiveDocument.ToggleFormsDesign.

What could be causing this? Has anyone else experienced this?

Here's a snippet of a macro:

Selection.Range.ContentControls.Add (wdContentControlText)
ActiveDocument.ToggleFormsDesign
Selection.TypeText Text:="Date"
Selection.MoveLeft Unit:=wdCharacter, Count:=4, Extend:=wdExtend
Selection.Style = ActiveDocument.Styles("TextRed")
ActiveDocument.ToggleFormsDesign

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瞳孔里扚悲伤 2024-09-16 04:52:08

原因是 Selection 对象在 ToggleDesignMode 之后丢失 - 这意味着不再有 Selection 对象。在您录制的示例中,您重新选择了键入“日期”的位置,但 Word 不知道该选择哪里。

解决这个问题的方法是使用记录的宏作为起点,然后进一步清理它们。像这样:

Sub InsertContentControl()
    Dim myDoc As Document
    Set myDoc = ActiveDocument
    Dim tr As Style
    Set tr = myDoc.Styles("TextRed"):
    Dim cc As ContentControl
    Dim sel As Range
    Set sel = Selection.Range
    Set cc = sel.ContentControls.Add(wdContentControlText)
    cc.SetPlaceholderText Text:="Date"
    cc.DefaultTextStyle = tr
End Sub

要使用新样式执行此操作,请使用以下命令:

Sub InsertContentControlwithNewStyle()
    Dim myDoc As Document
    Set myDoc = ActiveDocument
    Dim tr As Style
    Set tr = myDoc.Styles.Add("New TextRed")
    tr.BaseStyle = wdStyleNormal
    tr.Font.ColorIndex = wdRed
    Dim cc As ContentControl
    Dim sel As Range
    Set sel = Selection.Range
    Set cc = sel.ContentControls.Add(wdContentControlText)
    cc.SetPlaceholderText Text:="Date"
    cc.DefaultTextStyle = tr
End Sub

The reason is because the Selection object becomes lost after ToggleDesignMode - meaning there is no longer a Selection object. In your recorded example, you reselected the place in which to type "Date" but Word doesn't know where to select.

The way to get around this is to use recorded macros as a starting point, but then further clean them up. Like this:

Sub InsertContentControl()
    Dim myDoc As Document
    Set myDoc = ActiveDocument
    Dim tr As Style
    Set tr = myDoc.Styles("TextRed"):
    Dim cc As ContentControl
    Dim sel As Range
    Set sel = Selection.Range
    Set cc = sel.ContentControls.Add(wdContentControlText)
    cc.SetPlaceholderText Text:="Date"
    cc.DefaultTextStyle = tr
End Sub

To do this with a new style, use the following:

Sub InsertContentControlwithNewStyle()
    Dim myDoc As Document
    Set myDoc = ActiveDocument
    Dim tr As Style
    Set tr = myDoc.Styles.Add("New TextRed")
    tr.BaseStyle = wdStyleNormal
    tr.Font.ColorIndex = wdRed
    Dim cc As ContentControl
    Dim sel As Range
    Set sel = Selection.Range
    Set cc = sel.ContentControls.Add(wdContentControlText)
    cc.SetPlaceholderText Text:="Date"
    cc.DefaultTextStyle = tr
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文