CliveBurt01
New Member
- Joined
- Aug 6, 2014
- Messages
- 1
Dear Excel supremos
When a work sheet opens I want to test whether the person opening it has it opened as read only or not.
If they have the file as Read/Write, the macro goes away and records their logon id and when they opened the file, they should then go to the logon page, and detail any changes ot the spreadsheet that they have made in the area that has been set up for this .... a sort of version control, where at least I know who's opened the file as Read/Write ....
What I would like to do next is chancge the macro so that it tests whether the file has been opened as Read Only, and if it has, allow them access without recording anything .... i.e. they can look but not able to overwrite the file.
How do I do this?
The Macros/VBA I have written (with the help of a former colleague is as follows:
Option Explicit
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, _
nSize As Long) As Long
Sub Get_User_Name()
Dim lpbuff As String * 25
Dim ret As Long, UserName As String
ret = GetUserName(lpbuff, 25)
UserName = Left(lpbuff, InStr(lpbuff, Chr(0)) - 1)
MsgBox UserName
End Sub
Sub Autpen()
Dim lpbuff As String * 25
Dim ret As Long, UserName As String
Dim strName As String
ActiveWorkbook.ReadOnly
unprotect_log
ret = GetUserName(lpbuff, 25)
Worksheets("Log").Select
Range("a1").Select
Do Until ActiveCell.Value = ""
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Value = Left(lpbuff, InStr(lpbuff, Chr(0)) - 1)
ActiveCell.Offset(0, 1).Value = Now
ActiveCell.Offset(0, 1).Select
PROTECT_LOG
Worksheets(1).Select
End Sub
Sub PROTECT_LOG()
'
' PROTECT_LOG Macro
'
Worksheets("Log").Select
ActiveSheet.Protect
Application.Goto Reference:="R1C1"
ActiveWorkbook.Save
End Sub
Sub unprotect_log()
Worksheets("Log").Select
ActiveSheet.Unprotect
Application.Goto Reference:="R1C1"
ActiveWorkbook.Save
End Sub
Sub Auto_Close()
PROTECT_LOG
Worksheets(1).Select
Application.Goto Reference:="R1C1"
ActiveWorkbook.Save
End Sub
I realise that I will have to create an alternative Auto-Open() VBA routine, just not sure on how to test for the Read Only condition, and then what happens from there .....
Regards
Clive
When a work sheet opens I want to test whether the person opening it has it opened as read only or not.
If they have the file as Read/Write, the macro goes away and records their logon id and when they opened the file, they should then go to the logon page, and detail any changes ot the spreadsheet that they have made in the area that has been set up for this .... a sort of version control, where at least I know who's opened the file as Read/Write ....
What I would like to do next is chancge the macro so that it tests whether the file has been opened as Read Only, and if it has, allow them access without recording anything .... i.e. they can look but not able to overwrite the file.
How do I do this?
The Macros/VBA I have written (with the help of a former colleague is as follows:
Option Explicit
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, _
nSize As Long) As Long
Sub Get_User_Name()
Dim lpbuff As String * 25
Dim ret As Long, UserName As String
ret = GetUserName(lpbuff, 25)
UserName = Left(lpbuff, InStr(lpbuff, Chr(0)) - 1)
MsgBox UserName
End Sub
Sub Autpen()
Dim lpbuff As String * 25
Dim ret As Long, UserName As String
Dim strName As String
ActiveWorkbook.ReadOnly
unprotect_log
ret = GetUserName(lpbuff, 25)
Worksheets("Log").Select
Range("a1").Select
Do Until ActiveCell.Value = ""
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Value = Left(lpbuff, InStr(lpbuff, Chr(0)) - 1)
ActiveCell.Offset(0, 1).Value = Now
ActiveCell.Offset(0, 1).Select
PROTECT_LOG
Worksheets(1).Select
End Sub
Sub PROTECT_LOG()
'
' PROTECT_LOG Macro
'
Worksheets("Log").Select
ActiveSheet.Protect
Application.Goto Reference:="R1C1"
ActiveWorkbook.Save
End Sub
Sub unprotect_log()
Worksheets("Log").Select
ActiveSheet.Unprotect
Application.Goto Reference:="R1C1"
ActiveWorkbook.Save
End Sub
Sub Auto_Close()
PROTECT_LOG
Worksheets(1).Select
Application.Goto Reference:="R1C1"
ActiveWorkbook.Save
End Sub
I realise that I will have to create an alternative Auto-Open() VBA routine, just not sure on how to test for the Read Only condition, and then what happens from there .....
Regards
Clive