CheckBox1_Click()或CheckBox1_Change()没有实时响应

问题描述 投票:0回答:1

我一直在尝试使用Microsoft Word以及ActiveX内容控件复选框使以下VBA代码在点击时运行。

Private Sub CheckBox1_Click()

If CheckBox1.Value = True Then
ThisDocument.Tables(1).Rows(1).Range.HighlightColorIndex = wdBlack

ElseIf CheckBox1.Value = False Then
ThisDocument.Tables(1).Rows(1).Range.HighlightColorIndex = wdYellow

End If

End Sub

我在这里遇到的问题是,只有当我公开它并从开发人员下的宏手动运行它时,它才会运行。

我也尝试过以下方法

Private Sub CheckBox1_Change()

If CheckBox1.Value = True Then
ThisDocument.Tables(1).Rows(1).Range.HighlightColorIndex = wdBlack

ElseIf CheckBox1.Value = False Then
ThisDocument.Tables(1).Rows(1).Range.HighlightColorIndex = wdYellow

End If

End Sub

这里的问题是,它仅在我手动运行宏时才运行,但是在这种情况下,我只能运行一次,然后宏不起作用,除非我选中或取消选中该框,然后保存,关闭,重新打开并重新运行说宏。

我唯一想到的其他事情是这样的:

 Private Sub Document_open()  

    Call NameBox1
    Call CheckBox1_Click

    End Sub

    Private Sub CheckBox1_Click()

    If CheckBox1.Value = True Then
    ThisDocument.Tables(1).Rows(1).Range.HighlightColorIndex = wdBlack

    ElseIf CheckBox1.Value = False Then
    ThisDocument.Tables(1).Rows(1).Range.HighlightColorIndex = wdYellow

    End If

    End Sub

这可行,但这不是我想要做的,因为这意味着用户需要关闭并打开文档备份才能看到marco运行。

对此将提供任何帮助,我们将不胜感激。

vba ms-word
1个回答
0
投票

我刚开始工作就可以了。这个问题看起来很简单。

Public Sub CheckBox1_Click()

If CheckBox1.Value = True Then
ThisDocument.Tables(1).Rows(4).Shading.BackgroundPatternColor = wdColorRed


ElseIf CheckBox1.Value = False Then
ThisDocument.Tables(1).Rows(4).Shading.BackgroundPatternColor = wdColorWhite

End If

End Sub

看起来像不是使用.Range,我需要使用.Shading,它现在可以实时工作。我不确定为什么.Range不会对highlightcolorindex这样做,但是对于我的应用程序,上面的代码可以正常工作,并且确实满足我的需要。将继续为某人提供解释为什么.Range不允许宏实时运行的解释,因为这是实际的问题。

谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.