简体   繁体   English

VBA Access / Excel-切换读/写

[英]VBA Access/Excel - Toggle Read/Write

Using VBA Access 使用VBA访问

Is there a more efficient way of making an open read only excel file to read/write mode? 有没有更有效的方法将打开的只读excel文件设置为读/写模式? Or check if read only is true wait till read/write is active 或检查只读是否为真,直到读/写处于活动状态

I created a continuous loop that opens and closes the file till read/write is active. 我创建了一个连续循环,可以打开和关闭文件,直到激活读/写功能为止。 However sometimes it works sometimes it doesn't, frustrating. 但是有时它会起作用,有时却不会,令人沮丧。

I've looked into toggling read/write also Changefileaccess even SETATTR functions 我已经研究过切换读/写更改文件访问甚至SETATTR函数

Dim xl As Object

Set xl = CreateObject("Excel.Application")


Do Until xl.ActiveWorkbook.ReadOnly = False
xl.Quit
xl.Workbooks.Open ("C:\TEST\Test.xlsb")
If xl.ActiveWorkbook.ReadOnly = False Then Exit Do
Loop

Few observations: 很少观察到:

  1. There is a possibility that your code can go into an endless loop. 您的代码可能会陷入无限循环。 Give some wait time before you recheck again. 给您一些等待时间,然后再次检查。
  2. Define the number of times the code should attempt re-opening. 定义代码尝试重新打开的次数。
  3. Don't use CreateObject . 不要使用CreateObject CreateObject creates a new applicaiton. CreateObject创建一个新的应用程序。 Use GetObject if you want to work with the already open file. 如果要使用已打开的文件,请使用GetObject
  4. Check if the attribute is read only before you re-open the file. 重新打开文件之前,请检查该属性是否为只读。

See this example ( Untested ) 看到这个例子( 未经测试

Sub Sample()
    Dim objxlAp As Object, objxlWb As Object
    Dim FlName As String
    Dim NumberOfAttempt As Long

    FlName = "C:\TEST\Test.xlsb"

    Set objxlAp = GetObject(, "Excel.Application")
    Set objxlWb = objxlAp.ActiveWorkbook

    Do Until objxlWb.ReadOnly = False
        objxlWb.Close (False)

        If GetAttr(FlName) = vbReadOnly Then _
        SetAttr FlName, vbNormal

        objxlAp.Workbooks.Open (FlName)

        If objxlWb.ReadOnly = False Then Exit Do

        Wait 60 '<~~ Wait for 60 seconds. Change as Applicable
        NumberOfAttempt = NumberOfAttempt + 1

        If NumberOfAttempt > 5 Then
            MsgBox "Tried reopening the file 5 times. Unable to do it. Exiting the loop"
            Exit Do
        End If
    Loop
End Sub

Private Sub Wait(ByVal nSec As Long)
    nSec = nSec + Timer
    While nSec > Timer
        DoEvents
    Wend
End Sub

Important Note : A workbook can me made Read-Only using these two means 重要说明 :可以使用这两种方式将工作簿设为只读

  1. Right click on the file and set attribute as ReadOnly 右键单击该文件,然后将属性设置为ReadOnly
  2. File Save As - Read Only Recommended. 文件另存为-只读建议。 The above code is not for this method. 上面的代码不适用于此方法。

My Assumptions : 我的假设

You are not on a network where the file has been opened by a different user. 您不在由其他用户打开文件的网络上。

Can you check using GetAttr : 您可以使用GetAttr检查:

If ((GetAttr("C:\tmp\Test.xlsb")) And vbReadOnly) Then
Debug.Print "Do something, file is read only"
End If

Maybe change this property before open file? 也许在打开文件之前更改此属性?

Sub OpenRW()

Dim wb As Workbook

Set wb = Workbooks.Open("C:\tmp\Test.xlsb")
Debug.Print "1. ReadOnly? " & ActiveWorkbook.ReadOnly 'read only
wb.Close
SetAttr "C:\tmp\Test.xlsb", vbNormal
Set wb = Workbooks.Open("C:\tmp\Test.xlsb") 'read/write
Debug.Print "2. ReadOnly? " & ActiveWorkbook.ReadOnly
wb.Close
SetAttr "C:\tmp\Test.xlsb", vbReadOnly
Set wb = Workbooks.Open("C:\tmp\Test.xlsb")
Debug.Print "3. ReadOnly? " & ActiveWorkbook.ReadOnly 'read only
wb.Close

End Sub

Results: 结果:

OpenRW
1. ReadOnly? True
2. ReadOnly? False
3. ReadOnly? True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM