I try to lock a small dummy text file on OneDrive in memory, by opening it Read/Write, so that I have exclusive access over some shared Excel tables in another open shared xlsm. After the necessary modifications, I will close the file to allow other users to lock it and modify the shared xlsm if necessary.
The Open and FSO.OpenTextFile commands work as expected on local files. I cannot get either to work, though, with a "https://d.docs.live.net/....." filepath. They throw a runtime error.
Workbooks.Open can handle such pathnames (as I read and tested myself in another SO post) but I do not wish to open the file in Excel, merely keep it locked in memory on an OS level until I finish my modifications.
Public Sub GetExclusiveAccess()
Dim fso As FileSystemObject
Dim txtStream As TextStream
Dim filepath As String
Dim PathDelimiter As String
PathDelimiter = IIf(InStr(ThisWorkbook.Path, "//") > 0, "/", "\")
filepath = ThisWorkbook.Path & PathDelimiter & "filelock.txt"
' First method
Set fso = New FileSystemObject
'On Error Resume Next
Set txtStream = fso.OpenTextFile(filepath, ForWriting, False)
'On Error GoTo 0
If txtStream Is Nothing Then GoTo Error
'Success...
txtStream.Close
' Second method
Open filepath For Output As #1
'Success...
Close #1
Error:
End Sub