How to Send an Email via VBScript When Excel Workbook is Unprotected
Question details
The user needs a VBScript solution to monitor an Excel workbook and send an automated email alert if the workbook's structure protection has been removed.

- Product
- Microsoft Excel, Microsoft Outlook, VBScript
- Device & OS
- not provided
- Scenario
- Automating security monitoring for an Excel file to alert administrators when a user removes the workbook structure protection.
- Observed behavior
- The user requires an external script that evaluates the ProtectStructure property of an Excel file and triggers an Outlook email if the property returns False.
Ensure that both Microsoft Excel and Microsoft Outlook are installed and configured on your system, as this script relies on their COM objects to function properly.
Create a VBScript to Check Workbook Protection and Send Email
Write a standalone VBScript (.vbs) file that opens the specified Excel file, evaluates its protection status, and triggers an Outlook email if unprotected.
This approach uses Windows Script Host to invoke the Excel.Application and Outlook.Application COM objects. It operates independently of Excel VBA macros, making it ideal for external monitoring or scheduled tasks.
Open Notepad or your preferred plain text editor to begin creating the VBScript file.
Insert the following code into your file: Dim excelApp, workbook, outlookApp, mail Set excelApp = CreateObject("Excel.Application") Set workbook = excelApp.Workbooks.Open("C:\Path\To\Excel\File.xlsx") If workbook.ProtectStructure = False Then Set outlookApp = CreateObject("Outlook.Application") Set mail = outlookApp.CreateItem(0) mail.To = "recipient@example.com" mail.Subject = "Excel file unprotected" mail.Body = "The Excel file has been unprotected." mail.Send End If workbook.Close False excelApp.Quit Set mail = Nothing Set outlookApp = Nothing Set workbook = Nothing Set excelApp = Nothing
Modify the file path ("C:\Path\To\Excel\File.xlsx"), recipient email address, subject line, and body text to match your specific requirements.
Save the file with a .vbs extension (for example, CheckProtection.vbs). Double-click the file in Windows File Explorer to execute the script and test the automation.

Try WPS Office for Seamless Spreadsheet Management
If you frequently work with complex spreadsheets, automation, and macros, consider switching to WPS Office. It provides a lightweight, highly compatible environment for handling advanced Office tasks without the hefty subscription fees.

Frequently Asked Questions
Why is my script giving an 'ActiveX component can't create object' error?
This error typically occurs if Microsoft Excel or Outlook is not installed on the machine running the script, or if the COM registry keys for these applications are corrupted.
How can I automate this script to run every day?
You can use the Windows Task Scheduler to create a new task, set a daily trigger, and set the action to execute your saved .vbs file automatically in the background.
Does this script work if the Excel file is already open by another user?
If the file is locked by another user on a network, the script will open it in Read-Only mode. It can still check the ProtectStructure property, but ensure your script uses 'workbook.Close False' to close it without saving and avoid read-only prompt dialogs.




