Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
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.
You can apply the following VBA code to protect your specific file from being printed.



The following sample code illustrates how to prevent users from printing an Excel file:
#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();
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.