当工作簿受保护时样式子例程失败
Style Sub routine fails when workbook protected
我真的很难理解为什么在工作簿受到保护的情况下设置样式的子例程将不起作用。我试图在子例程之前解锁特定页面,但失败了。我曾尝试使用 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例程是这样的
Sub InputStyleRestore()
With ActiveWorkbook.Styles("Input")
.Interior.Color = 10079487
.Font.Color = -9027777
End With
End Sub
当工作簿被锁定时,我得到一个 "运行-Time error 1004 - Application-defined or object-defined Error" 和行 .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
编辑: 此解决方案解释了工作簿样式的结构和行为,以及为什么需要取消保护所有工作表才能修改任何样式。但是,为了避免需要不断修改工作簿样式,我建议创建两个样式,如 InputOn
和 InputOff
,并根据需要应用它们。这将消除修改样式的需要以及取消保护和保护所有工作表的要求。
要强调的几点:
提到 Workbook
保护,但是发布的代码不包含任何 Workbook.Protect
实例,而是应用的保护仅影响 Worksheets
Workbook_Open
事件试图保护所有应用 UserInterFaceOnly
属性 的工作表,但是它试图将此 属性 应用到 worksheets
已经受到保护,因此无法激活 属性。 Worksheets
必须先解除保护,然后再保护包括属性UserInterFaceOnly=TRUE
才有效。
Style object (Excel) 是 Styles object
的成员,虽然位于 Workbook
级别,但一旦 Worksheet
受到保护,整个 Styles collection
也受到保护。因此,即使要修改的 Style
未在任何 Worksheets
.
建议将这些更改应用于您的程序:
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
我真的很难理解为什么在工作簿受到保护的情况下设置样式的子例程将不起作用。我试图在子例程之前解锁特定页面,但失败了。我曾尝试使用 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例程是这样的
Sub InputStyleRestore()
With ActiveWorkbook.Styles("Input")
.Interior.Color = 10079487
.Font.Color = -9027777
End With
End Sub
当工作簿被锁定时,我得到一个 "运行-Time error 1004 - Application-defined or object-defined Error" 和行 .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
编辑: 此解决方案解释了工作簿样式的结构和行为,以及为什么需要取消保护所有工作表才能修改任何样式。但是,为了避免需要不断修改工作簿样式,我建议创建两个样式,如 InputOn
和 InputOff
,并根据需要应用它们。这将消除修改样式的需要以及取消保护和保护所有工作表的要求。
要强调的几点:
提到
Workbook
保护,但是发布的代码不包含任何Workbook.Protect
实例,而是应用的保护仅影响Worksheets
Workbook_Open
事件试图保护所有应用UserInterFaceOnly
属性 的工作表,但是它试图将此 属性 应用到worksheets
已经受到保护,因此无法激活 属性。Worksheets
必须先解除保护,然后再保护包括属性UserInterFaceOnly=TRUE
才有效。Style object (Excel) 是
Styles object
的成员,虽然位于Workbook
级别,但一旦Worksheet
受到保护,整个Styles collection
也受到保护。因此,即使要修改的Style
未在任何Worksheets
.
建议将这些更改应用于您的程序:
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