B
Berj
Dear Anyone,
I have an Excel file which I have prepared to sell to a company. But that
company may copy and give it to another company to use. Naturally, I don’t
want that to happen. If the second company wants it, they should buy it from
me. Copyrighting is not a solution in my case. I live in a place where I can
reach my customers personally.
As I do not know any programming in Excel, I asked somebody in these forums
on the net to prepare a code in Excel which, before opening the Excel file,
reads the hard disk serial number, compares it to a fixed serial in the code
in order to open the file. The fixed serial number of the hard disk and the
code will be typed by me when the file is first copied to a new hard disk.
From then on, it works fine. But when it is copied to a new hard disk
(someone steals it), it doesn’t open because the hard disk serial will be
different of course.
Well, someone answered, and after a few adjustments, the code was made. Good.
This is the code:
========================================
Public WithEvents App As Application
'---------------------------------------------------------------
Function DiskVolumeId(Drive As String) As String
'---------------------------------------------------------------
Dim sTemp As String
Dim iPos As Long
iPos = InStr(1, Drive, ":")
Drive = IIf(iPos > 0, Left(Drive, iPos), Drive & ":")
sTemp = Hex(CreateObject("Scripting.FileSystemObject") _
.Drives.Item(CStr(Drive)).SerialNumber)
DiskVolumeId = Left(sTemp, 4) & "-" & Right(sTemp, 4)
End Function
Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
If DiskVolumeId("C:") <> "E06F-9984" Then
Wb.Close savechanges:=False
End If
End Sub
Private Sub Workbook_Open()
Set App = Application
End Sub
Now, as I think of it, another possibility occurred to me. What if the
company’s computer was changed, or a problem happened and the hard disk has
to be formatted. In both these cases the hard disk serial number would
change, and the Excel file (even if it was backed up and restored to the new
hard disk) would not be opened. So, the company would lose the information
entered in the Excel file.
Now, I am in need of another addition to the already existing code which
works in the following way:
As the Excel file is opened, it checks and compares hard disk serial with
existing fixed serial in the code. If they match, the file is opened. If not,
instead of closing the file, it asks the question, “Serial number:â€. Here the
serial number of the new hard disk has to be entered. (Of course, an operator
would never guess that it is the hard disk serial number). From here on, two
possibilities exist:
a) If the serial number entered is exactly the same as the new hard disk
serial number, the Excel file opens, and the new serial is recorded in the
code such that when the file is opened again on the same hard disk, it never
asks for a serial number and opens the file without any problems.
b) If the serial number entered is not the serial number of the new hard
disk, it answers “wrong serial number†and doesn’t open the file without
giving a second chance.
I think maybe this code should work for any Microsoft Office document too.
I hope somebody understood me well and will reply soon.
Regards,
Berj
I have an Excel file which I have prepared to sell to a company. But that
company may copy and give it to another company to use. Naturally, I don’t
want that to happen. If the second company wants it, they should buy it from
me. Copyrighting is not a solution in my case. I live in a place where I can
reach my customers personally.
As I do not know any programming in Excel, I asked somebody in these forums
on the net to prepare a code in Excel which, before opening the Excel file,
reads the hard disk serial number, compares it to a fixed serial in the code
in order to open the file. The fixed serial number of the hard disk and the
code will be typed by me when the file is first copied to a new hard disk.
From then on, it works fine. But when it is copied to a new hard disk
(someone steals it), it doesn’t open because the hard disk serial will be
different of course.
Well, someone answered, and after a few adjustments, the code was made. Good.
This is the code:
========================================
Public WithEvents App As Application
'---------------------------------------------------------------
Function DiskVolumeId(Drive As String) As String
'---------------------------------------------------------------
Dim sTemp As String
Dim iPos As Long
iPos = InStr(1, Drive, ":")
Drive = IIf(iPos > 0, Left(Drive, iPos), Drive & ":")
sTemp = Hex(CreateObject("Scripting.FileSystemObject") _
.Drives.Item(CStr(Drive)).SerialNumber)
DiskVolumeId = Left(sTemp, 4) & "-" & Right(sTemp, 4)
End Function
Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
If DiskVolumeId("C:") <> "E06F-9984" Then
Wb.Close savechanges:=False
End If
End Sub
Private Sub Workbook_Open()
Set App = Application
End Sub
Now, as I think of it, another possibility occurred to me. What if the
company’s computer was changed, or a problem happened and the hard disk has
to be formatted. In both these cases the hard disk serial number would
change, and the Excel file (even if it was backed up and restored to the new
hard disk) would not be opened. So, the company would lose the information
entered in the Excel file.
Now, I am in need of another addition to the already existing code which
works in the following way:
As the Excel file is opened, it checks and compares hard disk serial with
existing fixed serial in the code. If they match, the file is opened. If not,
instead of closing the file, it asks the question, “Serial number:â€. Here the
serial number of the new hard disk has to be entered. (Of course, an operator
would never guess that it is the hard disk serial number). From here on, two
possibilities exist:
a) If the serial number entered is exactly the same as the new hard disk
serial number, the Excel file opens, and the new serial is recorded in the
code such that when the file is opened again on the same hard disk, it never
asks for a serial number and opens the file without any problems.
b) If the serial number entered is not the serial number of the new hard
disk, it answers “wrong serial number†and doesn’t open the file without
giving a second chance.
I think maybe this code should work for any Microsoft Office document too.
I hope somebody understood me well and will reply soon.
Regards,
Berj