Jay Baker
New Member
- Joined
- Dec 9, 2003
- Messages
- 15
I want to make sure that my workbooks aren't copied and opened on other computers. Notwithstanding the idea of "security" with an xls workbook (or lack thereof) I'm trying to write a macro that will allow a workbook to check and see if a certain file exists on a hard disk to allow it to open. The code I have butchered is:
Option Explicit
Private Const LINK_PATH As String = "C:"
Private Const FILE_NAME As String = "Book1.xls"
Private Sub Workbook_Open()
Select Case GetValue(LINK_PATH, FILE_NAME)
Case "File Not Found"
GoTo CloseMe
Case "OK"
GoTo ExitHere
End Select
ExitHere:
Exit Sub
CloseMe:
ThisWorkbook.Close False
GoTo ExitHere
HandleErr:
MsgBox Err.Description
Resume ExitHere
End Sub
'======== REGULAR MODULE (OR WORKBOOK MODULE) ========
Public Function GetValue(LINK_PATH, FILE_NAME)
' Taken from John Walkenbach's web site:
' http://www.j-walk.com/ss/excel/eee/eee009.txt ,
' this function will read the value from a closed
' workbook.
' Retrieves a value from a closed workbook
Dim arg As String
' Make sure the file exists
If Right(LINK_PATH, 1) <> "" Then LINK_PATH = LINK_PATH & ""
If Dir(LINK_PATH & FILE_NAME) = "" Then
GetValue = "File Not Found"
If Dir(LINK_PATH & FILE_NAME) <> "" Then
GetValue = "OK"
Exit Function
End If
End If
' Execute an XLM macro
GetValue = ExecuteExcel4Macro(arg)
End Function
Where I have tried C:Book1.xls as the file to check and needless to say it doesn't work.
This is stuff that I have tried to piece together from different posts, so if there is a completely different (read better) way to do this, please let me know.
Thanking in advance,
Jay
Option Explicit
Private Const LINK_PATH As String = "C:"
Private Const FILE_NAME As String = "Book1.xls"
Private Sub Workbook_Open()
Select Case GetValue(LINK_PATH, FILE_NAME)
Case "File Not Found"
GoTo CloseMe
Case "OK"
GoTo ExitHere
End Select
ExitHere:
Exit Sub
CloseMe:
ThisWorkbook.Close False
GoTo ExitHere
HandleErr:
MsgBox Err.Description
Resume ExitHere
End Sub
'======== REGULAR MODULE (OR WORKBOOK MODULE) ========
Public Function GetValue(LINK_PATH, FILE_NAME)
' Taken from John Walkenbach's web site:
' http://www.j-walk.com/ss/excel/eee/eee009.txt ,
' this function will read the value from a closed
' workbook.
' Retrieves a value from a closed workbook
Dim arg As String
' Make sure the file exists
If Right(LINK_PATH, 1) <> "" Then LINK_PATH = LINK_PATH & ""
If Dir(LINK_PATH & FILE_NAME) = "" Then
GetValue = "File Not Found"
If Dir(LINK_PATH & FILE_NAME) <> "" Then
GetValue = "OK"
Exit Function
End If
End If
' Execute an XLM macro
GetValue = ExecuteExcel4Macro(arg)
End Function
Where I have tried C:Book1.xls as the file to check and needless to say it doesn't work.
This is stuff that I have tried to piece together from different posts, so if there is a completely different (read better) way to do this, please let me know.
Thanking in advance,
Jay