代码改变世界

c#实现word,winWordControl 文档不允许复制、粘贴、隐藏工具栏、快捷保存

2013-10-23 11:12  Lecone.JY.HU  阅读(1375)  评论(0)    收藏  举报

1.隐藏工具栏

//隐藏工具栏
for (int i = 1; i <= winWordControl1.document.CommandBars.Count; i++) { winWordControl1.document.CommandBars[i].Enabled = false;//置灰
winWordControl1.document.CommandBars[i].Visible = false;//不可见
}

本语句完成的功能是对整体工具条的隐藏,如:常用,格式等工具条,而工具条内具体的功能点隐藏如下:

复制代码
 for(int i=1;i<=winWordControl1.document.ActiveWindow.Application.CommandBars["Standard"].Controls.Count;i++)
{
if ("打印(&P)打印预览(&V)".IndexOf(winWordControl1.document.ActiveWindow.Application.CommandBars["Standard"].Controls[i].Caption)!=-1) { winWordControl1.document.ActiveWindow.Application.CommandBars["Standard"].Controls[i].Enabled = true; } else { winWordControl1.document.ActiveWindow.Application.CommandBars["Standard"].Controls[i].Enabled = false; winWordControl1.document.ActiveWindow.Application.CommandBars["Standard"].Controls[i].Visible = false; } }
复制代码

本代码实现“常用”工具条,打印和打印预览显示、可操作。

2.不允许复制、粘贴

  object missing = Type.Missing;
  if (winWordControl1.document.Application.ActiveDocument.ProtectionType == Word.WdProtectionType.wdNoProtection)
  {
      winWordControl1.document.Application.ActiveDocument.Protect(Word.WdProtectionType.wdAllowOnlyFormFields, ref missing, ref missing);
  }

3.屏蔽快捷键方式

//不允许快捷键保存
int keyControl = winWordControl1.document.Application.BuildKeyCode(Word.WdKey.wdKeyControl, ref  missing, ref  missing, ref missing);
int cKey = winWordControl1.document.Application.BuildKeyCode(Word.WdKey.wdKeyS, ref  missing, ref  missing, ref missing);
winWordControl1.document.Application.get_FindKey(keyControl | cKey, ref missing).Disable();

屏蔽其他的快捷键参考上面代码就可。

编辑推荐:
· 一个字符串替换引发的性能血案:正则回溯与救赎之路
· 为什么说方法的参数最好不要超过4个?
· C#.Net 筑基-优雅 LINQ 的查询艺术
· 一个自认为理想主义者的程序员,写了5年公众号、博客的初衷
· 大数据高并发核心场景实战,数据持久化之冷热分离
阅读排行:
· 《HelloGitHub》第 111 期
· 使用GcExcel .NET将Excel导出为PDF
· MySQL 字符集、排序规则与查询关系详解
· 本地搭建一个对嘴AI工具
· 【设计模式】外观模式
的风格风格
点击右上角即可分享
微信分享提示