Search code examples
c#powerpointvstooffice-addinspowerpoint-addins

VSTO Add-in for Powerpoint to make Presentation Read-Only and Stop Printing


I've been trying to write add-ins for MS Word, Excel, and PowerPoint so I could have some control over print, save, and open events.

I have successfully done every part for word and excel but I can't find the right events and methods for PowerPoint.

In Word and Excel, I used BeforePrint Event which has the Cancel parameter, but the PresentationPrint Event doesn't have the Cancel parameter and I don't know how else I can stop the user from printing the presentation. Also, I used the Protect method in Word and Excel to make the documents and workbooks Read-Only, but I couldn't find any method that would make PowerPoint presentations Read-Only.


        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            Globals.ThisAddIn.Application.DocumentOpen += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentOpenEventHandler(this.Application_DocumentOpen);
            Globals.ThisAddIn.Application.DocumentBeforePrint += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforePrintEventHandler(this.Application_DocumentBeforePrint);
            Globals.ThisAddIn.Application.DocumentBeforeSave += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeSaveEventHandler(this.Application_DocumentBeforeSave);
        }


        //ReadOnly - Restrictions - when a Document is Opened
        public void Application_DocumentOpen(Microsoft.Office.Interop.Word.Document Doc)
        {
            IntPtr hWnd = Process.GetCurrentProcess().MainWindowHandle;

            if (E1(3, hWnd, Doc.FullName) == 0)
            {
                
            }
            else
            {

                MessageBox.Show("Read-Only on Open");
                object noReset = false;
                object password = "@Dministrat0r";
                object useIRM = false;
                object enforceStyleLock = false;

                Doc.Protect(Word.WdProtectionType.wdAllowOnlyReading, ref noReset, ref password, ref useIRM, ref enforceStyleLock);
            }

        }

        //Before Print
        public void Application_DocumentBeforePrint(Word.Document doc, ref bool Cancel)
        {

            IntPtr hWnd = Process.GetCurrentProcess().MainWindowHandle;

            if (E1(1, hWnd, doc.FullName) == 0)
            {
                
            }
            else
            {
                MessageBox.Show("Blocked Print");
                Cancel = true;
            }
        }

        //Before Save
        public void Application_DocumentBeforeSave(Microsoft.Office.Interop.Word.Document Doc, ref bool SaveAsUI, ref bool Cancel)
        {
            IntPtr hWnd = Process.GetCurrentProcess().MainWindowHandle;

            if (E1(2, hWnd, Doc.FullName) == 0)
            {

            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Blocked Saving");
                Cancel = true;
            }
        }

This is my code for Word and I would like to be able to do the same in PowerPoint.


Solution

  • Unfortunately, there's no BeforePrint event available in PowerPoint.

    As a possible workaround you may consider repurposing ribbon controls for printing and setting keyboard hooks for handling shortcuts. So, if a user decides to print the document by clicking on the ribbon button or backstage view element you will get the event first and will be able to cancel the default action, as well as for keyboard shortcuts.

    You can read how to repurpose ribbon controls in the Temporarily Repurpose Commands on the Office Fluent Ribbon article. And the take a look at the globalmousekeyhook repo on github for handling keyboard shortcuts.