我在Word 2013中有一个表单。它是各种表,我希望用户能够在表的底部添加一行。我用以下代码完成了这个:
`Dim oTable As table
Dim oCell As Cell
Dim oPrevRow as Row, oNewRow As Row
Dim iColumn As Long
Set oTable = ActiveDocument.tables (1)
Set oPrevRow = oTable.Rows(oTable.Rpws.Count)
oTable.Rows.Add
Set oNewRow = oTable.Rows(oTable.rows.Count)`
我想要的是新行中的所有7个单元格都插入了富文本内容控件。我该怎么做呢?
答案 0 :(得分:0)
以下代码适用于文档正文中任意位置标记为“TblBkMk”的表格。代码中的注释显示了如何测试特定表。只需将代码添加到文档或其模板的“ThisDocument”代码模块中。退出表中的最后一个内容控件时,宏会触发。该代码还规定文档具有“只读”或“填写表单”保护(如果您使用的话,将密码添加到指定的代码中)
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
'The following code conditionally adds a new row, with content controls, to the designated table.
Dim i As Long, j As Long, Prot As Long
Const Pwd As String = "" 'Insert password (if any) here
'Bookmarking the table provides the flexibility of being able to deal with the addition/deletion
' of other tables before the one we want to process.
Const StrBkMk As String = "TblBkMk"
'Exit if we're not in a table - we don't really need this is using a bookmarked table,
' but it's a safeguard against the bookmark having been expanded/moved.
If CCtrl.Range.Information(wdWithInTable) = False Then Exit Sub
With ActiveDocument
If .Bookmarks.Exists(StrBkMk) = False Then
MsgBox "The table bookmark: '" & StrBkMk & "' is missing." & vbCr & _
"Please add it to the relevant table before continuing.", vbExclamation
Exit Sub
End If
End With
With CCtrl
'Check that the Content Control is within our bookmarked table's range.
If .Range.InRange(ActiveDocument.Bookmarks(StrBkMk).Range) = False Then Exit Sub
' One could test for a particular table instead, in which case all the code dealing
' with wdWithInTable & StrBkMk can be deleted. For example:
'If .Range.InRange(ActiveDocument.Tables(1).Range) = False Then Exit Sub
'Get the number of ContentControls in the table
i = .Range.Tables(1).Range.ContentControls.Count
'Get our ContentControl's index # in the table
j = ActiveDocument.Range(.Range.Tables(1).Range.Start, .Range.End).ContentControls.Count
'Check that we're using the last content control
If i <> j Then Exit Sub
End With
'Solicit user input
If MsgBox("Add new row?", vbQuestion + vbYesNo) <> vbYes Then Exit Sub
With ActiveDocument
' Un-protect the document, if applicable
Prot = .ProtectionType
If .ProtectionType <> wdNoProtection Then
Prot = .ProtectionType
.Unprotect Password:=Pwd
End If
With Selection.Tables(1).Rows
'Insert an empty paragraph after our table, then replace it with a replica of the last row
With .Last.Range
.Next.InsertBefore vbCr
.Next.FormattedText = .FormattedText
End With
'Reset all content controls in the new last row
For Each CCtrl In .Last.Range.ContentControls
With CCtrl
If .Type = wdContentControlCheckBox Then .Checked = False
If .Type = wdContentControlRichText Or .Type = wdContentControlText Then .Range.Text = ""
If .Type = wdContentControlDropdownList Then .DropdownListEntries(1).Select
If .Type = wdContentControlComboBox Then .DropdownListEntries(1).Select
If .Type = wdContentControlDate Then .Range.Text = ""
End With
Next
End With
'Update the bookmarked range
.Bookmarks.Add Name:=StrBkMk, Range:=Selection.Tables(1).Range
' Re-protect the document, if applicable
.Protect Type:=Prot, Password:=Pwd
End With
End Sub