VBA-ContentControlOnEnter正在影响我的所有控件,而不仅仅是指定的控件

时间:2019-02-22 21:52:46

标签: vba ms-word

我已经为课程设置了可填写的表格。我正在尝试根据所选课程和开始日期自动填充课程结束日期。

它不会在进入“ Endate”控件时触发,而是在进入所有引发重复运行时错误的控件时触发。我愿意在单击时填充该字段,但这似乎不是一个选择。

Private Sub Document_ContentControlOnEnter(ByVal Endate As ContentControl)

'Declare variable names to deal with the content control data
Dim SD As ContentControl
Dim TC As ContentControl
Dim TC1 As ContentControl
Dim ED As ContentControl
Dim NewDate

'Connect each variable name to its content control
Set TC = ActiveDocument.SelectContentControlsByTag("Training").Item(1)
Set SD = ActiveDocument.SelectContentControlsByTag("Stardate").Item(1)
Set ED = ActiveDocument.SelectContentControlsByTag("Endate").Item(1)
'For some reason, twice removed from the source works better
Set TC1 = TC

    If SD.Range.Text <> "Click to enter a date" Then
    NewDate = DateValue(SD.Range.Text)

'Look at Training Content Control - what was selected.
'If one of the two longer courses is picked, add 2 days to the start date
'and use that as the end date. Otherwise just add one day.
    Select Case TC1.Range.Text
        Case "Basic Skills"
            ED.Range.Text = Format((NewDate + 2), "MMM d, yyyy")
        Case "Caseworker"
            ED.Range.Text = Format((NewDate + 2), "MMM d, yyyy")
        Case Else
            ED.Range.Text = Format((NewDate + 1), "MMM d, yyyy")
    End Select
    End If

'Once we're done, re-set the variables for the next round. This does not change
'the content of the form.
    Set TC = Nothing
    Set SD = Nothing
    Set ED = Nothing

End Sub

2 个答案:

答案 0 :(得分:1)

看到上述行为的原因

  

它不会在进入“ Endate”控件时被触发,而是   进入所有控件都会触发

是由于Microsoft对文档中包含的内容控件进行的设计决定。所有内容控件都会触发的每种事件类型都有一个事件。这意味着,如果代码特定于一个或几个内容控件,则需要评估触发事件的内容控件,并根据需要分支代码。

请注意,事件签名传递了参数ByVal ContentControl As ContentControl。这是触发事件的内容控件。因此,给此参数指定特定内容控件的名称并不是一种最佳方法,它可以是任何内容控件。

我修改了代码的第一部分,以说明如何构造它以测试哪个内容控件触发了该事件:

Private Sub Document_ContentControlOnEnter(ByVal cc As ContentControl)

'Declare variable names to deal with the content control data
Dim SD As ContentControl
Dim TC As ContentControl
Dim TC1 As ContentControl
Dim ED As ContentControl
Dim NewDate

If cc.Tag = "EndDate" Then
  'Connect each variable name to its content control
  Set TC = ActiveDocument.SelectContentControlsByTag("Training").Item(1)
  Set SD = ActiveDocument.SelectContentControlsByTag("Stardate").Item(1)
  Set ED = cc 
  'Do other things, here
End If
End Sub

答案 1 :(得分:0)

我根据事件的名称看到的是,每当您输入控件时,它都会将enddate控件传递给代码。基本上,只需检查并确认结束日期是否具有值,然后再进行其余操作即可。

Private Sub Document_ContentControlOnEnter(ByVal Endate As ContentControl)
If len(Endate.Range.Text) > 0 then
    'Declare variable names to deal with the content control data
    Dim SD As ContentControl
    Dim TC As ContentControl
    Dim TC1 As ContentControl
    Dim ED As ContentControl
    Dim NewDate

    'Connect each variable name to its content control
    Set TC = ActiveDocument.SelectContentControlsByTag("Training").Item(1)
    Set SD = ActiveDocument.SelectContentControlsByTag("Stardate").Item(1)
    Set ED = ActiveDocument.SelectContentControlsByTag("Endate").Item(1)
    'For some reason, twice removed from the source works better
    Set TC1 = TC

        If SD.Range.Text <> "Click to enter a date" Then
        NewDate = DateValue(SD.Range.Text)

    'Look at Training Content Control - what was selected.
    'If one of the two longer courses is picked, add 2 days to the start date
    'and use that as the end date. Otherwise just add one day.
        Select Case TC1.Range.Text
            Case "Basic Skills"
                ED.Range.Text = Format((NewDate + 2), "MMM d, yyyy")
            Case "Caseworker"
                ED.Range.Text = Format((NewDate + 2), "MMM d, yyyy")
            Case Else
                ED.Range.Text = Format((NewDate + 1), "MMM d, yyyy")
        End Select
        End If

    'Once we're done, re-set the variables for the next round. This does not change
    'the content of the form.
        Set TC = Nothing
        Set SD = Nothing
        Set ED = Nothing
end if
End Sub