Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 用于检查文件是否为只读的VBA代码_Excel_Vba - Fatal编程技术网

Excel 用于检查文件是否为只读的VBA代码

Excel 用于检查文件是否为只读的VBA代码,excel,vba,Excel,Vba,请告知如何在打开文件之前检查文件是否为只读 以下是我用来打开文件的代码: myFileNameDir = "H:\Shaikh_Gaus\scratch\VBA\Book16.xlsx" Workbooks.Open Filename:=myFileNameDir, UpdateLinks:=0 Set ws1 = Worksheets("Students") 检查或更改属性的方法: Option Explicit Sub testFileAttributes() Const F

请告知如何在打开文件之前检查文件是否为只读

以下是我用来打开文件的代码:

myFileNameDir = "H:\Shaikh_Gaus\scratch\VBA\Book16.xlsx"
Workbooks.Open Filename:=myFileNameDir, UpdateLinks:=0

Set ws1 = Worksheets("Students")

检查或更改属性的方法:

Option Explicit

Sub testFileAttributes()

    Const FILE_NAME As String = "C:\Test.txt"

    If isReadOnly(FILE_NAME) Then MsgBox "File is Read-only"

    If isOpenAsReadOnly Then MsgBox "File is open as Read-only"

    makeReadWrite FILE_NAME

    If Not isReadOnly(FILE_NAME) Then MsgBox "File is not Read-only"

End Sub



我不知道答案,但我玩了一会儿。也许这些信息会帮助其他人找到答案。我将一本书保存为只读,然后尝试使用
FSO
检查
文件.attributes
。它显示为非只读的意思。打开它时,提示我打开它
ReadOnly
。将文件属性更改为只读使得
FSO
将其显示为33,并且没有提示以只读方式打开它。除非将
Open
方法中的
ReadOnly
标志设置为
TRUE
,否则将忽略该标志。不确定是否有办法在wb打开之前进行检查:/这里也有同样的问题。对于那些想尝试它的人:您需要参考Microsoft脚本运行时。备注33=32+1,表示存档和只读。使用按位
运算符,您可以编写
isReadOnly=file.Attributes和1
可能会有帮助,但我无法获取Shell对象。这也可能有帮助:这是另一个问题,但要取消工作簿的保护,请转到“审阅”选项卡,右侧的最后一部分(更改)然后单击“保护工作簿”-您可以在此处输入密码。如果提供的答案解决了您最初的问题,请关闭此问题
Public Function isReadOnly(ByVal fName As String) As Boolean

    'vbNormal = 0, vbReadOnly = 1, vbHidden = 2, vbDirectory = 16

    if Len(fName) > 0 Then isReadOnly = GetAttr(fName) And vbReadOnly

End Function
Public Function isOpenAsReadOnly(Optional ByRef wb As Workbook = Nothing) As Boolean

    If wb Is Nothing Then Set wb = ActiveWorkbook

    isOpenAsReadOnly = wb.ReadOnly 'opened as read-only within Microsoft Excel

End Function
Public Sub makeReadWrite(ByVal fName As String)

    Const READ_ONLY = 1

    Dim fso As Object, fsoFile As Object

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fsoFile = fso.getFile(fName)

    With fsoFile
        If .Attributes And READ_ONLY Then .Attributes = .Attributes Xor READ_ONLY
    End With
End Sub