Excel 保护其他工作簿的共享

Excel 保护其他工作簿的共享,excel,vba,Excel,Vba,我正在尝试为excel工作簿创建一个新的保护,因为您知道excel的保护非常弱。 现在我需要为共享设置保护,这将为另一个工作簿而不是activeworkbook完成 假设代码在Test.xlsm中,我需要保护关闭的工作簿Sample.xlm 这是我创建的代码 Dim wb As Workbook Sub DoProtectionTask() Dim sPath As String sPath = ThisWorkbook.Path & "\Sample.xlsm"

我正在尝试为excel工作簿创建一个新的保护,因为您知道excel的保护非常弱。 现在我需要为共享设置保护,这将为另一个工作簿而不是activeworkbook完成

假设代码在Test.xlsm中,我需要保护关闭的工作簿Sample.xlm 这是我创建的代码

Dim wb As Workbook

Sub DoProtectionTask()
    Dim sPath As String
    sPath = ThisWorkbook.Path & "\Sample.xlsm"
    Set wb = Workbooks.Open(sPath)
    ProtectSharing True, "123", "456"
    wb.Close True
End Sub

Private Sub ProtectSharing(ByVal b As Boolean, ByVal sPass1 As String, ByVal sPass2 As String)
    Dim x
    If b Then
        On Error GoTo Skipper
        Application.DisplayAlerts = False
        Application.EnableEvents = False
            wb.ProtectSharing Password:=sPass1, SharingPassword:=sPass2
Skipper:
        Application.DisplayAlerts = True
        Application.EnableEvents = True
    Else
        x = InputBox("Enter Admin Password")
        If x <> sPass1 Then MsgBox "Invalid Password. Contact The Workbook Owner", vbExclamation: Exit Sub
        wb.UnProtectSharing sPass1
    End If
End Sub
Dim wb As工作簿
子数据保护任务()
像细绳一样暗淡
sPath=ThisWorkbook.Path&“\Sample.xlsm”
设置wb=工作簿。打开(sPath)
保护共享为真,“123”、“456”
wb.Close为真
端接头
私有子保护共享(ByVal b作为布尔值,ByVal sPass1作为字符串,ByVal sPass2作为字符串)
暗x
如果b那么
关于错误转到跳过程序
Application.DisplayAlerts=False
Application.EnableEvents=False
wb.protect共享密码:=sPass1,共享密码:=sPass2
船长:
Application.DisplayAlerts=True
Application.EnableEvents=True
其他的
x=输入框(“输入管理员密码”)
如果是x sPass1,则MsgBox“密码无效。请与工作簿所有者联系”,VBEQUOTE:Exit Sub
wb.unprotect共享sPass1
如果结束
端接头
对于代码来说,它可以正常工作并且会出错,但是在运行代码后打开Sample.xlsm时,我没有发现工作簿受到了共享保护。尽管此方法适用于ThisWorbook或ActiveWorkbook


有什么想法吗?

我不确定自己是否理解得很好,但我认为这个代码
wb.unprotectShareing sPass1
应该是这样的:
wb.unprotectShareing sPass2

所有代码:

Dim wb As Workbook

Sub DoProtectionTask()
    Dim sPath As String
    sPath = ThisWorkbook.Path & "\Sample.xlsm"
    Set wb = Workbooks.Open(sPath)
    ProtectSharing True, "123", "456"
    wb.Close True
End Sub

Private Sub ProtectSharing(ByVal b As Boolean, ByVal sPass1 As String, ByVal sPass2 As String)
    Dim x
    If b Then
        On Error GoTo Skipper
        Application.DisplayAlerts = False
        Application.EnableEvents = False
            wb.ProtectSharing Password:=sPass1, SharingPassword:=sPass2
Skipper:
        Application.DisplayAlerts = True
        Application.EnableEvents = True
    Else
        x = InputBox("Enter Admin Password")
        If x <> sPass1 Then MsgBox "Invalid Password. Contact The Workbook Owner", vbExclamation: Exit Sub
        wb.UnProtectSharing sPass2
    End If
End Sub
Dim wb As工作簿
子数据保护任务()
像细绳一样暗淡
sPath=ThisWorkbook.Path&“\Sample.xlsm”
设置wb=工作簿。打开(sPath)
保护共享为真,“123”、“456”
wb.Close为真
端接头
私有子保护共享(ByVal b作为布尔值,ByVal sPass1作为字符串,ByVal sPass2作为字符串)
暗x
如果b那么
关于错误转到跳过程序
Application.DisplayAlerts=False
Application.EnableEvents=False
wb.protect共享密码:=sPass1,共享密码:=sPass2
船长:
Application.DisplayAlerts=True
Application.EnableEvents=True
其他的
x=输入框(“输入管理员密码”)
如果是x sPass1,则MsgBox“密码无效。请与工作簿所有者联系”,VBEQUOTE:Exit Sub
wb.unprotect共享sPass2
如果结束
端接头

我不确定自己是否理解清楚,但我认为此代码
wb.unprotectShareing sPass1
应该是这样的:
wb.unprotectShareing sPass2

所有代码:

Dim wb As Workbook

Sub DoProtectionTask()
    Dim sPath As String
    sPath = ThisWorkbook.Path & "\Sample.xlsm"
    Set wb = Workbooks.Open(sPath)
    ProtectSharing True, "123", "456"
    wb.Close True
End Sub

Private Sub ProtectSharing(ByVal b As Boolean, ByVal sPass1 As String, ByVal sPass2 As String)
    Dim x
    If b Then
        On Error GoTo Skipper
        Application.DisplayAlerts = False
        Application.EnableEvents = False
            wb.ProtectSharing Password:=sPass1, SharingPassword:=sPass2
Skipper:
        Application.DisplayAlerts = True
        Application.EnableEvents = True
    Else
        x = InputBox("Enter Admin Password")
        If x <> sPass1 Then MsgBox "Invalid Password. Contact The Workbook Owner", vbExclamation: Exit Sub
        wb.UnProtectSharing sPass2
    End If
End Sub
Dim wb As工作簿
子数据保护任务()
像细绳一样暗淡
sPath=ThisWorkbook.Path&“\Sample.xlsm”
设置wb=工作簿。打开(sPath)
保护共享为真,“123”、“456”
wb.Close为真
端接头
私有子保护共享(ByVal b作为布尔值,ByVal sPass1作为字符串,ByVal sPass2作为字符串)
暗x
如果b那么
关于错误转到跳过程序
Application.DisplayAlerts=False
Application.EnableEvents=False
wb.protect共享密码:=sPass1,共享密码:=sPass2
船长:
Application.DisplayAlerts=True
Application.EnableEvents=True
其他的
x=输入框(“输入管理员密码”)
如果是x sPass1,则MsgBox“密码无效。请与工作簿所有者联系”,VBEQUOTE:Exit Sub
wb.unprotect共享sPass2
如果结束
端接头

这并不能解决问题。问题是通过共享进行保护,而不是取消保护。这并不能解决问题。问题在于通过共享进行保护,而不是取消保护。