Home » excel » c# – How to protect my excel file from reading by the others?

c# – How to protect my excel file from reading by the others?

Posted by: admin May 14, 2020 Leave a comment

Questions:

Basically, I want to develop an office excel addin. Once you’ve installed this addin, all the excel files created can only be opened by a user whose excel has this addin installed. So I have two questions here:

  1. Can I use VBA to develop it or I have to use C# or VB to develop it as an application level addin?
  2. How to do it? It seems like I have to modify excel file format a little bit when saving, so that the other excel without this addin will deem that this is a broken file. Is there any excel API doing this? If not, do you have some better ideas?
How to&Answers:

Beautiful Question 🙂

Yes it can achieved in VBA Excel Add-In. In fact I did implement something similar several years ago. Here is the logic explained in 4 steps.

When you create the file through the Add-In, follow these steps

1) Save the file in the Temp directory as say Temp.xls and password protect it with a password. You can either choose to keep a Fixed Password or a Dynamic one. See this code to create a dynamic password.

Function GenerateRandPassword() As String
    Dim strEnvPass As String, strPass As String
    Dim n As Integer

    strEnvPass = "@#$%^"
    n = Application.WorksheetFunction.RandBetween(1, 10)

    For i = 1 To 5
        strPass = strPass & n
    Next i

    strPass = strEnvPass & strPass & strEnvPass

    GenerateRandPassword = strPass
End Function

To test it simply call it like this

Sub Sample()
    Debug.Print GenerateRandPassword
End Sub

Passwords will be in this format

@#$%^[email protected]#$%^
@#$%^[email protected]#$%^
@#$%^[email protected]#$%^
.
.
.
@#$%^[email protected]#$%^

When you are trying to open the file through the Add-In, loop through 1 to 10 and use @#$%^ as the suffix and the prefix to create the password and then try opening the file. That ways your Excel files will have random passwords. See 4th point on how to open these kind of files.

IMP: Don’t select too big a number in Application.WorksheetFunction.RandBetween(1, 10) It will only delay the opening of the file later on.

2) To get the temp directory use this code

Private Declare Function GetTempPath _
Lib "kernel32" Alias "GetTempPathA" _
(ByVal nBufferLength As Long, _
ByVal lpBuffer As String) As Long

Private Const MAX_PATH As Long = 260

Function TempPath() As String
    TempPath = String$(MAX_PATH, Chr$(0))
    GetTempPath MAX_PATH, TempPath
    TempPath = Replace(TempPath, Chr$(0), "")
End Function

Sub TmpPath()
    'This will give the Temp Path
    MsgBox TempPath
End Sub

3) Once the file is created in the temp directory, rename the file and give it an extension which Excel doesn’t recognize for example MyFile.ice (Took the 1st 3 letters from your SO handle iceagle)

You can use this code to rename the file

Name TempPath & "MyFile.xls" As "C:\MyFile.Ice"

This way normal users will have no idea which application is needed to open this file.

To open this file, you can set the filter as *.ice in the Application.GetOpenFilenam

Sub Sample()
    fileToOpen = Application.GetOpenFilename("ICE Files (*.ice), *.ice")
    If fileToOpen <> False Then
        Application.DisplayAlerts = False
        Workbooks.Open (fileToOpen)
        Application.DisplayAlerts = True
    End If
End Sub

4) To open the file use this code

Sub OpenFile()
    Dim wb As Workbook
    Dim strPass As String

    For i = 1 To 10
        On Error Resume Next
        strPass = "@#$%^" & i & i & i & i & i & "@#$%^"
        Set wb = Workbooks.Open("C:\MyFile.ice", , , , strPass)
        If Err.Number = 0 Then Exit For
        Err.Clear
    Next
    On Error GoTo 0
End Sub

Hope this gets you started 🙂