代码之家  ›  专栏  ›  技术社区  ›  Saxman

在允许VBA的同时限制Word文档

  •  1
  • Saxman  · 技术社区  · 6 年前

    我希望限制word文档的编辑,这样用户只能填写表单。我已经相应地设置了限制编辑选项。但是,根据表单上某些组合框中选择的选项,有VBA代码可在页面上添加其他控件。当我打开限制时,在特定选择上运行的VBA将不会运行,并抛出“命令不可用”错误。除了内容控件、切换框和组合框之外,还有什么方法可以限制文档的人类用户,并允许VBA代码自由操作,不受限制地操纵文档的格式?谢谢你的帮助!

    1 回复  |  直到 6 年前
        1
  •  3
  •   macropod    6 年前

    宏需要暂时删除保护,以便进行处理,然后恢复保护。例如:

    Dim lProt As Long: Const Pwd As String = "Password"
    With ActiveDocument
      If .ProtectionType <> wdNoProtection Then
        lProt = .ProtectionType
        .Unprotect Password:=Pwd
      End If
      'insert your code for content control additions here
      If lProt <> wdNoProtection Then .Protect Type:=lProt, NoReset:=True, Password:=Pwd
    End With