Programmatically protect an Excel file

Copper Contributor

Hello,

 

I need to be able to protect Excel files with password as seen here: https://support.microsoft.com/en-us/office/protect-an-excel-file-7359d4ae-7213-4ac2-b058-f75e9311b59...

 

But, I need to do it programmatically.

 

I have a PHP app that creates Excel files, then saves them, then allows users to download them.

 

I want to be able to add password to them via the PHP code.

 

I know I can't do it directly via PHP, but, is there any other language I can write a password protecting script, then run the script via the PHP code? For example writing some bash script, and using PHP's `exec` to run it. Or any other language. (Even if it would be some other file type, just that I can run it from the PHP side)

 

 

1 Reply

@Exlypter 

You can achieve programmatically protecting Excel files with a password by using a scripting language that can interact with the Excel application. One such scripting language is Visual Basic for Applications (VBA), which is built into Excel.

Here's an example of how you can create a VBA script to password protect an Excel file, and then execute it from PHP using the exec function:

  1. Create a new Excel file and save it with the desired name.
  2. Press Alt+F11 to open the VBA editor in Excel.
  3. Insert a new module by clicking on "Insert" in the menu and selecting "Module".
  4. In the module, paste the following VBA code:

vbacode

Sub ProtectExcelFile()
    Dim filePath As String
    Dim password As String
    
    ' Set the file path of the Excel file to be protected
    filePath = "C:\path\to\your\excel_file.xlsx"
    
    ' Set the desired password for protection
    password = "your_password"
    
    ' Open the workbook with the provided file path and password protect it
    Workbooks.Open filePath
    ActiveWorkbook.Password = password
    ActiveWorkbook.Save
    ActiveWorkbook.Close
    
    ' Close the Excel application
    Application.Quit
End Sub
  1. Modify the filePath variable to the actual path of your Excel file.
  2. Modify the password variable to the desired password you want to use for protection.
  3. Save the VBA code.
  4. Close the VBA editor.
  5. In your PHP code, use the exec function to execute the Excel application and run the VBA script. Here's an example:

php code

<?php
$excelFilePath = "C:\path\to\your\excel_file.xlsx";
$vbaScriptPath = "C:\path\to\your\vba_script.xlsm";

// Copy the Excel file to a new file with the .xlsm extension to enable macros
$macroEnabledFilePath = $excelFilePath . "m";
copy($excelFilePath, $macroEnabledFilePath);

// Create a separate workbook to hold the VBA script
copy($vbaScriptPath, "vba_script.xlsm");

// Execute the VBA script using the Excel application
exec("excel.exe vba_script.xlsm");

// Delete the temporary workbook and VBA script file
unlink($macroEnabledFilePath);
unlink("vba_script.xlsm");
?>
  1. Modify the $excelFilePath variable to the actual path of your Excel file.
  2. Modify the $vbaScriptPath variable to the actual path of the VBA script file (the file with the VBA code you created).
  3. Save the PHP code and execute it to protect the Excel file with the specified password.

This approach allows you to execute the VBA script from PHP by using the exec function and interacting with the Excel application. The text, steps and code/functions were created with the help of AI. My answers are voluntary and without guarantee!

 

Hope this will help you.