如何使VBA宏窗体可扩展?

问题描述 投票:0回答:1

上下文:

我相对较不熟悉使用VBA宏,特别是对于开发MS Word文档的宏。今天,我参与了一个小项目,发现该项目对我自己很有用,因为我参与了学校俱乐部的赞助工作,这是一种管理所需信息的好简单方法。 (此表格只是生成一个pdf文件,我可以将其发送给赞助我俱乐部的公司。我还计划将该信息存储在excel表格中,以方便保存记录和跟踪资金!)

我正在寻找指导的是使底部(表格)底部的“项目”部分具有灵活性的最佳方法是:

如果一家公司要赞助我们多个项目(例如,捐赠1笔500美元,然后再捐赠10块建筑材料),那么为其他项目添加新内容控件的最佳方法是什么?我目前有表单设置,以便可以处理一种类型的捐赠。

我知道我可以在模板文件中添加新的内容控件,然后在表单上使用条件控件向需要的内容控件添加其他捐赠类型/金额,但我想知道是否有比“隐藏”内容控件在众目?之下?就像我只在需要它们时创建它们一样,而不是将它们放在模板中,而可能不用。

我也知道我可以在模板中创建书签,然后根据表单中的条件控件在这些书签位置输入信息,但是我觉得这与上面的情况类似,尽管可能更“干净” 。

关于仅根据需要添加新的内容控件或书签的任何建议,将非常感谢,因为我想确保在我掌握更多和新的VBA技能时,我正在使用良好的做法并试图尽早推广自己。

发票模板和用户表单的屏幕截图

enter image description here

vba ms-word bookmarks contentcontrol
1个回答
0
投票

代替使用用户窗体,您可能可以使用表中的内容控件来完成文档本身中的全部工作,并带有如下所示的宏,当您退出表中的最后一个内容控件时会触发以下宏。宏将插入到文档的“ 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 Variant
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"
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 range.
  ' One could test for a particular table instead
  If .Range.InRange(ActiveDocument.Bookmarks(StrBkMk).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
  ' Re-protect the document, if applicable
  .Protect Type:=Prot, Password:=Pwd
End With
End Sub

注:上面的代码假定该表已标记为'TblBkMk'。根据代码中的注释,这样就可以在感兴趣的表之前插入/删除其他表。如果您对此不关心,请参阅:https://www.msofficeforums.com/word-vba/27809-code-add-new-row-table.html#post87989

有关一些演示文档,请参阅:

© www.soinside.com 2019 - 2024. All rights reserved.