Reputation: 17
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.
Upvotes: 1
Views: 359
Reputation: 49455
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.
Upvotes: 0
Reputation: 65702
I found this code here and am taking a copy to preserve it forever: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/ee285a3c-649a-41d6-9e08-36dfc3054bcd/disable-power-point-save-save-as-copy-and-print?forum=worddev
You can try to set permissions for users to read, write, print etc. For that you can use Presentation.Permission Property.
Sub AddUserPermissions()
Dim myPres As PowerPoint.Presentation
Dim myPer As Office.Permission
Dim NewOwnerPer As Office.UserPermission
Set myPres = Application.Presentations.Add(msoTrue)
Set myPer = myPres.Permission
myPer.Enabled = True
Set NewOwnerPer = myPer.Add("[email protected]", msoPermissionRead )
MsgBox(myPer(1).UserId + " " + Str(myPer(1).Permission)
MsgBox myPer(2).UserId + " " + Str(myPer(2).Permission)
End Sub
so that only particular users can access the document and perform the task according to their permissions.
Reference:
Presentation.Permission Property (PowerPoint)
TIP: Its very easy to convert VBA to C# VSTO code, the object model is exactly the same.
Upvotes: 0