【问题标题】:Word 2016- With multiple RSCCs, how to delete selection but prevent first section from being deleted in a protected document (VBA)?Word 2016-使用多个 RSCC,如何删除选择但防止在受保护文档 (VBA) 中删除第一部分?
【发布时间】:2020-11-05 04:02:05
【问题描述】:

我有一个包含多个 RepeatingSectionItems 的文档。以下代码用于在文档受到保护时删除当前选择。但是,我正在尝试找出如何防止第一个部分被选中,因为如果我删除了第一个重复部分,我将无法将其取回并且它会搞砸一切。

Application.ActiveDocument.Unprotect "green"
'
 Set objCC = ActiveDocument.SelectContentControlsByTitle("General").Item(1)
  objCC.LockContents = False
  objCC.AllowInsertDeleteSection = True
  '
 Dim CC As ContentControl
   If Selection.Information(wdInContentControl) Then
      Set CC = Selection.ParentContentControl
      If Not CC.Type = wdContentControlRepeatingSection Then
         Do Until CC.Type = wdContentControlRepeatingSection
            Set CC = CC.ParentContentControl
         Loop
      End If
      'loop through the repeatingsectionitems to find the one that selection is in
      Dim rsi As RepeatingSectionItem
      For Each rsi In CC.RepeatingSectionItems
         If Selection.Range.InRange(rsi.Range) Then
            rsi.Delete
            Exit For
         End If
      Next rsi
   End If
'
Set objCC = ActiveDocument.SelectContentControlsByTitle("General").Item(1)
  objCC.LockContents = True
  objCC.AllowInsertDeleteSection = False
 '
Application.ActiveDocument.Protect wdAllowOnlyFormFields, Password:="green"

【问题讨论】:

  • 这很简单。很抱歉直言不讳,但如果您在获得所有帮助后仍无法自己弄清楚这一点,那么您真的不应该尝试这种性质的项目。
  • 您是否注意到当您保护表单时,内容控件变得不可编辑?这是因为传统的 Word 表单是为使用旧版表单字段而非内容控件而构建的。
  • @John Korchok- 我明白了。所以您是说在尝试保护文档时使用旧版表单比使用内容控件更好?
  • @Timothy Rylatt- 我明白了。好吧,我想我会试着弄清楚。我的意思是我必须从某个地方开始。尽我所能的先生。
  • 如果您使用的是内容控件,则适当的保护是使用带有异常(保护区异常)的只读:support.microsoft.com/en-us/office/…

标签: vba ms-word


【解决方案1】:

下面的代码循环遍历RepeatingSectionItems的集合,如果索引等于一则显示一个消息框,如果索引大于一则只删除该项。

'loop through the repeatingsectionitems to find the one that selection is in
Dim index As Long
For index = 1 To cc.RepeatingSectionItems.Count
    If Selection.Range.InRange(cc.RepeatingSectionItems(index).Range) Then
        If index > 1 Then
            cc.RepeatingSectionItems(index).Delete
        Else
            MsgBox Prompt:="You are attempting to delete the first item.", Title:="Unable to Delete Item"
        End If
        Exit For
    End If
Next index

【讨论】:

  • 谢谢先生救了我。您能否指点我一个交互式资源,让我开始学习 vba 的基础知识?您在之前的问题中提供的链接将我发送到 Microsoft 网站,我很难遵循这些链接。再次,很抱歉给您带来不便,先生。
  • @MohamadBachrouche - 我已经很长时间没有学习了,那时还没有互动资源。您可以从VBA documentation 中学到很多东西,因为它还涵盖了基本概念。否则,请尝试询问 Google 或 Bing。
  • @TimothyRylatt- 啊谢谢。那么在我获得更多经验之前,我是否可以再次在这里发布问题?
  • @MohamadBachrouche - 只要您的问题与guidelines 相符,您就可以随意发布。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-29
  • 1970-01-01
  • 1970-01-01
  • 2015-02-26
  • 1970-01-01
相关资源
最近更新 更多