我创建了一个文本文件"C:UsersPublicMusic请勿删除—Sourcefile for Salary Sheet.txt"该txt文件的密码为1行。我想使用该密码并将其与ActiveSheet.Protect Password:=""
合并我希望能够使用TXT文件中的字符串来保护/取消保护工作表。
我试过下面的
Dim hFile As Long
Dim strFile As String
Dim strData As String * 4
hFile = FreeFile
strFile = "C:UsersPublicMusicDo Not Delete -- Sourcefile for Salary Sheet.txt"
Open strFile For Binary Access Read As hFile Len = 4
Get hFile, 1, strData
Activesheet.protect.password=(strData)
这些和各种不同的变化的结果如下-
"strData"用作密码
表被锁定,没有密码<</p>
似乎什么都不起作用。任何帮助吗?
一个函数(GetPassword)从文件中检索密码,然后SheetProtection切换保护
Public Sub test()
SheetProtection True
End Sub
Public Sub SheetProtection(active As Boolean)
Dim sh As Worksheet
Dim password As String
Set sh = ActiveSheet
password = GetPassword()
If active Then
sh.Protect password
Else
sh.Unprotect password
End If
End Sub
Public Function GetPassword() As String
Dim ff As Long
Dim fileName As String
Dim password As String
fileName = "C:UsersPublicMusicDo Not Delete -- Sourcefile for Salary Sheet.txt"
ff = FreeFile
Open fileName For Binary As #ff
password = Space(LOF(ff))
Get #ff, , password
Close #ff
GetPassword = password
End Function