无法关闭 Word VBA 中的设计模式
我对这件事感到困惑 - 在录制宏时,我基本上添加了一些字段并进入设计模式以便能够替换占位符的虚拟文本。现在,我在录制宏时退出设计模式,一切似乎都工作正常。但在播放宏时,它会在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
原因是
Selection
对象在ToggleDesignMode
之后丢失 - 这意味着不再有Selection
对象。在您录制的示例中,您重新选择了键入“日期”的位置,但 Word 不知道该选择哪里。解决这个问题的方法是使用记录的宏作为起点,然后进一步清理它们。像这样:
要使用新样式执行此操作,请使用以下命令:
The reason is because the
Selection
object becomes lost afterToggleDesignMode
- meaning there is no longer aSelection
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:
To do this with a new style, use the following: