我一直在检查工作簿是否存在工作表或单元格中的内容,而无需使用此命令打开工作簿
f = "'" & strFilePath1 & "[" & strFileType & "]" & strSheetName & "'!" & Range(strCell).Address(True, True, -4150)
CheckCell = Application.ExecuteExcel4Macro(f)
,它一直运行良好,但是现在我想检查一下工作表是否未打开就受到密码保护,但是没有成功。有人知道这是否可能吗?
谢谢您的帮助
答案 0 :(得分:6)
是的!是可能的。我很久以前就发现了如何做。我怀疑这在网络上的任何地方都被提及...
基本介绍:众所周知,直到2007版本的Microsoft Excel都使用一种称为Excel Binary File Format(.XLS)的专有二进制文件格式作为其主要格式。 Excel 2007及更高版本使用 Office Open XML 作为其主要文件格式,这是一种基于XML的格式,此格式是在Excel 2002中首次引入的基于XML的先前格式“ XML Spreadsheet”(“ XMLSS”)之后的一种。
逻辑:要了解其工作原理,请执行以下操作
package main
import "fmt"
func allLongestStrings(inputArray []string) []string {
maxLength := len(inputArray[0])
outputArray := []string{}
for _, value := range inputArray {
if len(value) > maxLength {
maxLength = len(value)
}
}
for _, val := range inputArray {
if len(val) == maxLength {
outputArray = append(outputArray, val)
}
}
return outputArray
}
func main() {
xs := []string{"aa", "aab", "bcd", "a", "cdf", "bb"}
fmt.Println(allLongestStrings(xs))
}
密码保护第一张纸blank
密码保护第三张纸any
,然后关闭文件Book1.xlsx
Book1.Zip
您将看到工作簿中的所有工作表都保存为\xl\worksheets
,Sheet1.xml
和Sheet2.xml
右键单击工作表,然后在记事本/ notepad ++中打开
您会注意到,所有受保护的工作表都有一个单词Sheet3.xml
,如下所示
因此,如果我们能够以某种方式检查相关表格中是否包含该单词,那么我们可以确定该表格是否受到保护。
代码:
这是可以帮助您实现目标的功能
<sheetProtection
注意:已针对'~~> API to get the user temp path
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" _
(ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Const MAX_PATH As Long = 260
Sub Sample()
'~~> Change as applicable
MsgBox IsSheetProtected("Sheet2", "C:\Users\routs\Desktop\Book1.xlsx")
End Sub
Private Function IsSheetProtected(sheetToCheck As Variant, FileTocheck As Variant) As Boolean
'~~> Temp Zip file name
Dim tmpFile As Variant
tmpFile = TempPath & "DeleteMeLater.zip"
'~~> Copy the excel file to temp directory and rename it to .zip
FileCopy FileTocheck, tmpFile
'~~> Create a temp directory
Dim tmpFolder As Variant
tmpFolder = TempPath & "DeleteMeLater"
'~~> Folder inside temp directory which needs to be checked
Dim SheetsFolder As String
SheetsFolder = tmpFolder & "\xl\worksheets\"
'~~> Create the temp folder
Dim FSO As Object
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(tmpFolder) = False Then
MkDir tmpFolder
End If
'~~> Extract zip file in that temp folder
Dim oApp As Object
Set oApp = CreateObject("Shell.Application")
oApp.Namespace(tmpFolder).CopyHere oApp.Namespace(tmpFile).items
'~~> Loop through that folder to work with the relevant sheet (file)
Dim StrFile As String
StrFile = Dir(SheetsFolder & sheetToCheck & ".xml")
Dim MyData As String, strData() As String
Dim i As Long
Do While Len(StrFile) > 0
'~~> Read the xml file in 1 go
Open SheetsFolder & StrFile For Binary As #1
MyData = Space$(LOF(1))
Get #1, , MyData
Close #1
strData() = Split(MyData, vbCrLf)
For i = LBound(strData) To UBound(strData)
'~~> Check if the file has the text "<sheetProtection"
If InStr(1, strData(i), "<sheetProtection", vbTextCompare) Then
IsSheetProtected = True
Exit For
End If
Next i
StrFile = Dir
Loop
'~~> Delete temp file
On Error Resume Next
Kill tmpFile
On Error GoTo 0
'~~> Delete temp folder.
FSO.deletefolder tmpFolder
End Function
'~~> Get User temp directory
Function TempPath() As String
TempPath = String$(MAX_PATH, Chr$(0))
GetTempPath MAX_PATH, TempPath
TempPath = Replace(TempPath, Chr$(0), "")
End Function
和.xlsx
文件进行了测试。