我一直在尝试为 MS Word、Excel 和 PowerPoint 编写加载项,这样我就可以对打印、保存和打开事件进行一些控制。
我已经成功地完成了word和excel的每个部分,但是我找不到适合PowerPoint的事件和方法。
在 Word 和 Excel 中,我使用了具有 Cancel 参数的 BeforePrint 事件,但是 PresentationPrint 事件没有 Cancel 参数,我不知道我还能如何阻止用户打印演示文稿。另外,我在 Word 和 Excel 中使用了Protect 方法将文档和工作簿设置为只读,但我找不到任何可以将 PowerPoint 演示文稿设置为只读的方法。
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;
}
}
这是我的 Word 代码,我希望能够在 PowerPoint 中执行相同的操作。
很遗憾,PowerPoint 中没有可用的
BeforePrint
事件。
作为一种可能的解决方法,您可以考虑将功能区控件重新用于打印和设置键盘挂钩以处理快捷方式。因此,如果用户决定通过单击功能区按钮或后台视图元素来打印文档,您将首先获得该事件,并且能够取消默认操作以及键盘快捷键。
您可以在 Temporarily Repurpose Commands on the Office Fluent Ribbon 文章中阅读如何重新调整功能区控件的用途。并查看 github 上的 globalmousekeyhook repo 以处理键盘快捷键。
我在这里找到这段代码并复制一份以永久保存它: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
您可以尝试为用户设置读取、写入、打印等权限。为此,您可以使用 Presentation.Permission 属性。
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
这样只有特定的用户才能访问文档并根据他们的权限执行任务。
参考:
Presentation.Permission 属性(PowerPoint)
TIP:VBA转C#VSTO代码非常简单,对象模型完全一样