How to Prevent Users from Printing Excel File with C++
Possible Usage Scenarios
In our daily work, there may be some important information in the Excel file; in order to protect the internal data from spreading, the company will not allow us to print them. This document will tell you how to prevent others from printing Excel files.
How to Prevent Users from Printing File in MS-Excel
You can apply the following VBA code to protect your specific file from being printed.
- Open your workbook which you don’t allow others to print.
- Select the “Developer” tab in the Excel ribbon and click on the “View Code” button in the “Controls” section. Alternatively, you can hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.
- Then in the left Project Explorer, double-click on ThisWorkbook to open the module and add some VBA codes.
- Then save and close this code, go back to the workbook, and now when you print the sample file, it will not be allowed to be printed, and you will get the following warning box:
How to Prevent Users from Printing Excel File using Aspose.Cells for C++
The following sample code illustrates how to prevent users from printing an Excel file:
- Load the sample file.
- Get the VbaModuleCollection object from the VbaProject property of the Workbook.
- Get the VbaModule object via “ThisWorkbook” name.
- Set the codes property of VbaModule.
- Save the sample file to xlsm format.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Create workbook from existing Excel file
Workbook workbook(u"Sample.xlsx");
// Access VBA project modules
VbaModuleCollection modules = workbook.GetVbaProject().GetModules();
// Set VBA code for 'ThisWorkbook' module
modules.Get(u"ThisWorkbook").SetCodes(u"Private Sub Workbook_BeforePrint(Cancel As Boolean)\r\n Cancel = True\r\n MsgBox \"Refusing to print in paperless office\"\r\nEnd Sub\r\n");
// Save the workbook as macro-enabled Excel file
workbook.Save(u"out.xlsm");
std::cout << "VBA code added and workbook saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}