设置Excel保护属性来自HTA

时间:2014-08-14 16:26:00

标签: vba vbscript hta

如何在HTA上的vbscript中使用以下内容。

ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFiltering:=True

如果我尝试使用":=" ,它会引发页面错误。

谢谢, 阿南德

2 个答案:

答案 0 :(得分:0)

“移植”VBA代码,如

ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFiltering:=True

要在.HTA(主机:mshta.exe)或.VBS(主机c / wscript.exe)中使用的VBScript,你必须

  1. 创建Excel.Application COM对象
  2. 使用它和它的工作簿/工作表集合深入查看要在其上调用方法的对象
  3. 将VBA的命名参数转换为VBScript的位置参数(基于该方法的文档)
  4. 定义xlXXXX常量
  5. 开始研究here

答案 1 :(得分:0)

我找到了另一种方法。我将所需的代码插入到对象Excel的VB代码模块中。 像下面的东西。

With myReport.VBProject.VBComponents("ThisWorkbook").CodeModule
    .InsertLines .CountOfLines + 1, _ 
    "Private Sub Workbook_Open()" & Chr(13) & _
    " ProtectMe(1)" & vbNewLine & _
    "End Sub" & vbNewLine & _
    "Sub ProtectMe(Status)" & vbNewLine & _
    "   Dim mySheet As Worksheet" & vbNewLine & _
    "   Dim myPassword " & vbNewLine & _
    "   myPassword = ""IamGenius""" & vbNewLine & _
    "   For Each mySheet In ThisWorkbook.Worksheets" & vbNewLine & _
    "       mySheet.Protect Password:=myPassword, DrawingObjects:=True, _" & vbNewLine & _
    "       Contents:=True, Scenarios:=True, AllowFormattingCells:=True, _" & vbNewLine & _
    "       AllowFormattingColumns:=True, AllowFormattingRows:=True, AllowFiltering:=True" & vbNewLine & _
    "       mySheet.EnableSelection = xlUnlockedCells" & vbNewLine & _
    "   Next mySheet" & vbNewLine & _
    "End Sub"          
End With

谢谢, 阿南德:)