MsgBox 如果 ContentControl 输入未得到答复
MsgBox if ContentControl input left unanswered
我有一个 MS Word 文档,我要求用户在各种 ContentControls 中从 1-5 中选择一个数值来回答几个问题。
我在文档中有一个名为 Calculate 的按钮。当用户单击此按钮时,代码会计算他们输入的平均值并将结果输入到各种带有书签的单元格中。
除非某些 ContentControl 留空,否则此方法有效。我希望在一个或多个 ContentControl 输入未得到答复时出现一个弹出框。
Private Sub Calculate_Click()
Dim ccs1 As ContentControls
Dim cc1 As ContentControl
Dim content1 As String
Dim ccs2 As ContentControls
Dim cc2 As ContentControl
Dim content2 As String
Dim MissingData As String
Dim TotalRating As Double
Dim OrgRating As Double
Dim TeamRating As Double
Dim StratRating As Double
Dim PandPRating As Double
Dim EvidenceRating As Double
Dim ESGRating As Double
' Error if Rating1 to Rating 19 are PlaceholderText
For i = 1 To 19
TagName1 = "Rating" & i
TagName2 = "RT" & i
Set doc = ActiveDocument
Set ccs1 = doc.SelectContentControlsByTag(TagName1)
Set cc1 = ccs1(1)
content1 = cc1.Range
Set ccs2 = doc.SelectContentControlsByTag(TagName2)
Set cc2 = ccs2(1)
content2 = cc2.Range
If content1 = cc1.PlaceholderText Then
MissingData = MissingData & vbCrLf & "- " & content2
End If
Next i
If MissingData <> "" Then
MsgBox "Please enter the following before submitting:" & MissingData
End
End If
Call updateDouble(ActiveDocument, "Rating", 1, 18, "TotalRating")
Call updateDouble(ActiveDocument, "Rating", 1, 4, "OrgRating")
Call updateDouble(ActiveDocument, "Rating", 5, 7, "TeamRating")
Call updateDouble(ActiveDocument, "Rating", 8, 10, "StratRating")
Call updateDouble(ActiveDocument, "Rating", 11, 14, "PandPRating")
Call updateDouble(ActiveDocument, "Rating", 15, 18, "EvidenceRating")
Call updateDouble(ActiveDocument, "Rating", 19, 19, "ESGRating")
End Sub
Private Sub updateDouble(doc As Word.Document, CCTitlePrefix As String, _StartNum As Integer, EndNum As Integer, CellName As String)
Dim i As Integer
Dim Total As Double
Total = 0
With doc
For i = StartNum To EndNum
Total = Total + CDbl(.SelectContentControlsByTitle(CCTitlePrefix & CStr(i))(1).Range.Text)
Next
.Bookmarks(CellName).Range.Paragraphs(1).Range.Text = CStr(Total / (1 + (EndNum - StartNum)))
End With
End Sub
注释掉的行是错误处理的开始。
我没有收到 MsgBox 弹出窗口,代码无法计算平均值。
尝试改变
If content1 = cc1.PlaceholderText Then
MissingData = MissingData & vbCrLf & "- " & content2
End If
至
If cc1.ShowingPlaceholderText Then
MissingData = MissingData & vbCrLf & "- " & content2
End If
我有一个 MS Word 文档,我要求用户在各种 ContentControls 中从 1-5 中选择一个数值来回答几个问题。
我在文档中有一个名为 Calculate 的按钮。当用户单击此按钮时,代码会计算他们输入的平均值并将结果输入到各种带有书签的单元格中。
除非某些 ContentControl 留空,否则此方法有效。我希望在一个或多个 ContentControl 输入未得到答复时出现一个弹出框。
Private Sub Calculate_Click()
Dim ccs1 As ContentControls
Dim cc1 As ContentControl
Dim content1 As String
Dim ccs2 As ContentControls
Dim cc2 As ContentControl
Dim content2 As String
Dim MissingData As String
Dim TotalRating As Double
Dim OrgRating As Double
Dim TeamRating As Double
Dim StratRating As Double
Dim PandPRating As Double
Dim EvidenceRating As Double
Dim ESGRating As Double
' Error if Rating1 to Rating 19 are PlaceholderText
For i = 1 To 19
TagName1 = "Rating" & i
TagName2 = "RT" & i
Set doc = ActiveDocument
Set ccs1 = doc.SelectContentControlsByTag(TagName1)
Set cc1 = ccs1(1)
content1 = cc1.Range
Set ccs2 = doc.SelectContentControlsByTag(TagName2)
Set cc2 = ccs2(1)
content2 = cc2.Range
If content1 = cc1.PlaceholderText Then
MissingData = MissingData & vbCrLf & "- " & content2
End If
Next i
If MissingData <> "" Then
MsgBox "Please enter the following before submitting:" & MissingData
End
End If
Call updateDouble(ActiveDocument, "Rating", 1, 18, "TotalRating")
Call updateDouble(ActiveDocument, "Rating", 1, 4, "OrgRating")
Call updateDouble(ActiveDocument, "Rating", 5, 7, "TeamRating")
Call updateDouble(ActiveDocument, "Rating", 8, 10, "StratRating")
Call updateDouble(ActiveDocument, "Rating", 11, 14, "PandPRating")
Call updateDouble(ActiveDocument, "Rating", 15, 18, "EvidenceRating")
Call updateDouble(ActiveDocument, "Rating", 19, 19, "ESGRating")
End Sub
Private Sub updateDouble(doc As Word.Document, CCTitlePrefix As String, _StartNum As Integer, EndNum As Integer, CellName As String)
Dim i As Integer
Dim Total As Double
Total = 0
With doc
For i = StartNum To EndNum
Total = Total + CDbl(.SelectContentControlsByTitle(CCTitlePrefix & CStr(i))(1).Range.Text)
Next
.Bookmarks(CellName).Range.Paragraphs(1).Range.Text = CStr(Total / (1 + (EndNum - StartNum)))
End With
End Sub
注释掉的行是错误处理的开始。
我没有收到 MsgBox 弹出窗口,代码无法计算平均值。
尝试改变
If content1 = cc1.PlaceholderText Then
MissingData = MissingData & vbCrLf & "- " & content2
End If
至
If cc1.ShowingPlaceholderText Then
MissingData = MissingData & vbCrLf & "- " & content2
End If