我在试图弄清楚如何编码时遇到了一些麻烦。我有一个包含5列的零件表(数量,零件号,描述,金额和总计)。单击命令按钮时,将添加新行。每个新添加的单元格都包含一个文本内容控件,每次添加新行时,该控件的名称和编号都会增加1。例如,Qty3,Qty4,Qty5等.Anount和Total列执行相同的操作。我锁定了总内容控件。
我已经发现在删除行之前必须解锁“Total”内容控件。现在,代码正在选择“Total3”内容控件,并成功删除该行,因为它是表中的最后一行。我无法弄清楚的部分是如何使代码识别表中最后一个“Total#”内容控件。我希望这是有道理的,如果需要,我可以清理任何事情。提前谢谢!
'Add Part Button
Private Sub CommandButton1_Click()
Dim oTable As Table
Set oTable = ActiveDocument.Tables(8)
MakePartsRow oTable
lbl_Exit:
Set oTable = Nothing
Exit Sub
End Sub
'Delete Part Button
Private Sub CommandButton11_Click()
Dim oTable As Table
Dim CC As ContentControl
Set oTable = ActiveDocument.Tables(8)
Dim lngRow As Long
Dim Index As Long
Dim sName1 As String
If Not ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Unprotect
End If
If oTable.Rows.Count > 2 Then
Set CC = ActiveDocument.SelectContentControlsByTag("Total3").Item(1)
With CC
.LockContents = False
.Delete True
End With
oTable.Rows.Last.Delete
End If
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset:=True, _
Password:=""
End Sub
Sub MakePartsRow(oTable As Table)
Dim oNewRow As Row
Dim oRng As Range
Dim oCell As Cell
Dim iCell As Integer
Dim oCC As ContentControl, oCC1 As ContentControl, oCC2 As ContentControl
Dim lngCell1 As Long, lngCell2 As Long
lngCell1 = 0: lngCell2 = 0
If Not ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Unprotect
End If
Set oNewRow = oTable.Rows.Add
oNewRow.Range.Font.Bold = False
For iCell = 1 To 5
Set oCell = oNewRow.Cells(iCell)
Set oRng = oCell.Range
oRng.End = oRng.End - 1
Select Case iCell
Case 1
Set oCC = oRng.ContentControls.Add(wdContentControlText)
With oCC
.Range.Text = "1"
.SetPlaceholderText , , ("Qty")
.Tag = "Qty" & oCell.RowIndex
End With
Case 2
Set oCC = oRng.ContentControls.Add(wdContentControlText)
With oCC
.SetPlaceholderText , , ("Part No.")
End With
Case 3
Set oCC = oRng.ContentControls.Add(wdContentControlText)
With oCC
.SetPlaceholderText , , ("Description")
End With
Case 4
Set oCC = oRng.ContentControls.Add(wdContentControlText)
With oCC
.Range.Text = ""
.SetPlaceholderText , , ("Amount")
.Tag = "Amount" & oCell.RowIndex
End With
Case 5
Set oCC = oRng.ContentControls.Add(wdContentControlText)
With oCC
.SetPlaceholderText , , ("Total")
.Tag = "Total" & oCell.RowIndex
End With
End Select
Next iCell
oNewRow.Cells(4).Select
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset:=True, _
Password:=""
End Sub
答案 0 :(得分:0)
我建议在CommandButton11_Click()子句中使用Total *等标记计算内容控件的数量,即:
internal class MyTesting : INotifyPropertyChanged
{
private string _CityName;
public MyTesting()
{
}
public string CityName
{
get { return _CityName; }
set { _CityName = value; OnPropertyChanged("CityName"); }
}
#region PropertyChangedEventHandler
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
#region " RaisePropertyChanged Function "
/// <summary>
///
/// </summary>
/// <param name="propertyName"></param>
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
#endregion
}
通过这种方式,您可以了解有多少 Total 控件,并且可以使用
引用最后一个 Total 控件。Dim ControlCount as Long
Dim ControlIndex as Long
ControlIndex = 0
For ControlIndex = 1 to ActiveDocument.ContentControls.Count
If ActiveDocument.ContentControls(ControlIndex).Tag Like "Total*" Then ControlCount = ControlCount + 1
Next ControlIndex
而不是你的:
Set CC = ActiveDocument.SelectContentControlsByTag("Total" & ControlCount).Item(1)