首页 > 解决方案 > 工作簿受保护时样式子例程失败

问题描述

我真的很难理解为什么如果工作簿受到保护,设置样式的子例程将不起作用。我曾尝试在子例程之前解锁特定页面,但这失败了。我尝试使用UserInterFaceOnly:=True锁定工作簿,但这也不起作用!

我为打开工作簿时设置了这个例程。

Private Sub Workbook_Open()
    Dim ws As Worksheet
    
    
    For Each ws In Worksheets

        ws.Protect Password:="Password", _
        UserInterFaceOnly:=True
    Next ws

    Sheets("Menu").Select
    Range("A1").Select
    
End Sub

我的子程序是这个

Sub InputStyleRestore()
  With ActiveWorkbook.Styles("Input")
    .Interior.Color = 10079487
    .Font.Color = -9027777
    
  End With
End Sub

当工作簿被锁定时,我收到“运行时错误 1004 - 应用程序定义或对象定义错误”并且该行 .Interior.Color = 10079487被突出显示。

我希望暂时解锁页面的例程的开始是这样开始的

Sub PartialPrintFamForm()



Dim FTW As Long
Dim myVariable As Long
Dim IsCreated As Boolean
Dim i As Long
Dim PdfFile As String, Title As String
Dim OutlApp As Object

    ActiveSheet.Unprotect Password:="Password"
    InputStyleRestore
' Employee Name as Title
  Title = Range("E21")

    If ActiveSheet.Name = "Caledonian Road Fam Form" Then
        myVariable = Sheets("Caledonian Road Fam Form").Range("R21").Value
        Sheets("Data Input").Range("B1310").Value = WorksheetFunction.Match(Sheets("Caledonian Road Fam Form").Range("O21").Value, Sheets("Data Input").Range("B1:B1000"), 0)
        FTW = Sheets("Data Input").Range("B1310").Value
        Sheets("Data Input").Cells(FTW, 25) = myVariable
        MsgBox "The First page only will now print out for you."
        InputStyleClear

是否有另一种我不知道的方法可以使例程锁定工作簿,因为它在手动解锁时工作正常。

这是 InputStyleClear 子例程

Sub InputStyleClear()


        'Prevent Computer Screen from running
        Application.ScreenUpdating = False
        
        
  
    With ActiveSheet.Name
            .Unprotect Password:="Pampigny", _
                DrawingObjects:=False, Contents:=False, _
                Scenarios:=False, UserInterFaceOnly:=False
             
    End With
    
    With ActiveSheet.Styles("Input")
            .Interior.Pattern = xlNone
            .Font.ColorIndex = xlAutomatic
            .Borders(xlLeft).LineStyle = xlNone
            .Borders(xlRight).LineStyle = xlNone
            .Borders(xlTop).LineStyle = xlNone
            .Borders(xlBottom).LineStyle = xlNone
            
    End With
    
    With ActiveSheet.Name
        
                .Protect Password:="Pampigny", _
                DrawingObjects:=True, Contents:=True, _
                Scenarios:=True, UserInterFaceOnly:=True
    End With

                   
  
        'Allow Computer Screen to refresh (not necessary in most cases)
         Application.ScreenUpdating = True
End Sub

标签: excelvbaruntime-errorpassword-protection

解决方案


编辑:此解决方案解释了工作簿样式的结构和行为,以及为什么需要取消保护所有工作表以修改任何样式。但是,为了避免需要不断地修改工作簿样式,我建议创建两个样式,例如InputOn& InputOff,并根据需要应用它们。这将消除修改样式的需要以及取消保护和保护所有工作表的要求。


需要强调的几点:

  1. 提及Workbook保护,但发布的代码不包含任何实例,Workbook.Protect而应用的保护仅影响Worksheets

  2. Workbook_Open事件尝试保护所有应用该UserInterFaceOnly属性的工作表,但它尝试将此属性应用于worksheets已受保护的工作表,因此无法激活该属性。Worksheets必须首先不受保护,然后保护包括财产才能UserInterFaceOnly=TRUE生效。

  3. Style 对象 (Excel)是的成员,Styles object虽然位于Workbook级别,但一旦 aWorksheet受到保护,整个Styles collection对象也将受到保护。因此,即使要修改Style 没有在任何.WorksheetsStyleWorksheets

建议将这些更改应用于您的程序:

Private Sub Workbook_Open()
Rem Using Sheet instead of Worksheet to care for Charts in the workbook if any
Dim Sht As Object
    With ThisWorkbook
        For Each Sht In .Sheets
            With Sht
                .Unprotect Password:="Password"
                .Protect Password:="Password", _
                    DrawingObjects:=True, Contents:=True, _
                    Scenarios:=True, UserInterFaceOnly:=True
        End With: Next
    
        With .Sheets("Menu")
            Activate
            Application.Goto .Cells(1), 1
    
    End With: End With

    End Sub

在过程中PartialPrintFamForm替换这些行:

  ActiveSheet.Unprotect Password:="Password"
  InputStyleRestore

用这些:

With ThisWorkbook
    For Each Sht In .Sheets
        Sht.Unprotect Password:="Password"
Next: End With

InputStyleRestore

With ThisWorkbook
    For Each Sht In .Sheets
        With Sht
            .Protect Password:="Password", _
                DrawingObjects:=True, Contents:=True, _
                Scenarios:=True, UserInterFaceOnly:=True
End With: Next: End With

推荐阅读