我已经为课程设置了可填充表格。我试图根据所选课程和开始日期自动填充课程日期。
,它没有在输入"端"控件时触发,而是触发输入所有会引发重复运行时错误的控件。我愿意在点击时填充该字段,但这似乎不是一个选择。
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
您看到所描述的行为
的原因而不是在输入"端"控件时触发它,而是 输入所有控件的触发器
是由于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
我基于事件的名称所看到的是,每当您输入控件时,它都会将端模控件传递给代码。基本上,只需检查并查看终点是否具有价值,然后再进行其余部分即可。
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