在宏之后取消选择内容控件的 Word 宏
Word Macro to Deselect a content control after macro
我当前的项目是为一位同事列一个清单,因为我喜欢 over-complicate 一切,所以我是 运行 宏,供他在完成工作时核对。当他完成时,工作描述将从 header 风格变为正常,然后更新 ToC。所有这一切都有效,但我有时会遇到内容控件保持选中状态的问题。我通常可以毫无问题地选中和取消选中它一两次,但最终由于某种原因光标不会移出复选框,因此后续点击不会触发 OnEnter
.
Private Sub Document_ContentControlOnEnter(ByVal CCtrl As ContentControl)
With CCtrl
If .Type = wdContentControlCheckBox Then
If .Checked = True Then
.Range.Paragraphs.First.Style = wdStyleNormal
ActiveDocument.TablesOfContents(1).Update
Selection.Collapse direction:=wdCollapseEnd
Else
.Range.Paragraphs.First.Style = wdStyleHeading2
ActiveDocument.TablesOfContents(1).Update
Selection.MoveRight 1
End If
End If
End With
End Sub
有没有办法强制 word 取消选择内容控件并将光标移动到同一行的某个位置?
我尝试了 Selection.MoveDown 1
、Selection.Collapse direction:=wdCollapseEnd
和 Selection.MoveEnd
,但 none 有效。
您可以利用以下事实:通过内容控件的范围可以访问包含它的对象。比如可以"drill up"到内容控件所在的段落:
CCtrl.Range.Paragraphs(1).Range.Characters.Last.Select
这也可以是段落中的任何字符。以下(在我的测试中)将选择内容紧跟在内容控件之后:
CCtrl.Range.Paragraphs(1).Range.Characters(4).Select
合并到您的代码中:
Private Sub Document_ContentControlOnEnter(ByVal CCtrl As ContentControl)
With CCtrl
If .Type = wdContentControlCheckBox Then
If .Checked = True Then
.Range.Paragraphs.First.Style = wdStyleNormal
ActiveDocument.TablesOfContents(1).Update
Selection.Collapse direction:=wdCollapseEnd
Else
.Range.Paragraphs.First.Style = wdStyleHeading2
ActiveDocument.TablesOfContents(1).Update
Selection.MoveRight 1
End If
'Select the last character in the paragraph (the paragraph mark)
CCtrl.Range.Paragraphs(1).Range.Characters.Last.Select
'Remove the selection, so the cursor blinks at the end of the paragraph
Selection.Collapse
End If
End With
End Sub
我当前的项目是为一位同事列一个清单,因为我喜欢 over-complicate 一切,所以我是 运行 宏,供他在完成工作时核对。当他完成时,工作描述将从 header 风格变为正常,然后更新 ToC。所有这一切都有效,但我有时会遇到内容控件保持选中状态的问题。我通常可以毫无问题地选中和取消选中它一两次,但最终由于某种原因光标不会移出复选框,因此后续点击不会触发 OnEnter
.
Private Sub Document_ContentControlOnEnter(ByVal CCtrl As ContentControl)
With CCtrl
If .Type = wdContentControlCheckBox Then
If .Checked = True Then
.Range.Paragraphs.First.Style = wdStyleNormal
ActiveDocument.TablesOfContents(1).Update
Selection.Collapse direction:=wdCollapseEnd
Else
.Range.Paragraphs.First.Style = wdStyleHeading2
ActiveDocument.TablesOfContents(1).Update
Selection.MoveRight 1
End If
End If
End With
End Sub
有没有办法强制 word 取消选择内容控件并将光标移动到同一行的某个位置?
我尝试了 Selection.MoveDown 1
、Selection.Collapse direction:=wdCollapseEnd
和 Selection.MoveEnd
,但 none 有效。
您可以利用以下事实:通过内容控件的范围可以访问包含它的对象。比如可以"drill up"到内容控件所在的段落:
CCtrl.Range.Paragraphs(1).Range.Characters.Last.Select
这也可以是段落中的任何字符。以下(在我的测试中)将选择内容紧跟在内容控件之后:
CCtrl.Range.Paragraphs(1).Range.Characters(4).Select
合并到您的代码中:
Private Sub Document_ContentControlOnEnter(ByVal CCtrl As ContentControl)
With CCtrl
If .Type = wdContentControlCheckBox Then
If .Checked = True Then
.Range.Paragraphs.First.Style = wdStyleNormal
ActiveDocument.TablesOfContents(1).Update
Selection.Collapse direction:=wdCollapseEnd
Else
.Range.Paragraphs.First.Style = wdStyleHeading2
ActiveDocument.TablesOfContents(1).Update
Selection.MoveRight 1
End If
'Select the last character in the paragraph (the paragraph mark)
CCtrl.Range.Paragraphs(1).Range.Characters.Last.Select
'Remove the selection, so the cursor blinks at the end of the paragraph
Selection.Collapse
End If
End With
End Sub