无法使用 C# 禁用文件菜单 - Word 2016
Unable to disable File Menu with C# - Word 2016
我目前正在尝试禁用 Microsoft Word 2016 文件菜单中的几个命令。
目前我有:
Main.cs:
protected override Office.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
return new Ribbon();
}
Ribbon.cs:
public string ReturnUI()
{
return GetSource("Ribbon.xml");
}
private static string GetSource(string sourceName)
{
Assembly asm = Assembly.GetExecutingAssembly();
string[] sourceNames = asm.GetManifestSourceNames();
for (int x = 0; x < sourceNames.Length; ++x)
{
if (string.Compare(sourceName, sourceNames[x], StringComparison.OrdinalIgnoreCase) == 0)
{
using (StreamReader sr = new StreamReader(asm.GetManifestSourceStream(sourceNames[x])))
{
if (sr != null)
{
return sr.ReadToEnd();
}
}
}
}
return "";
}
Ribbon.xml:
<commands>
<command idMso="FileSaveAs" enabled="false" />
</commands>
<backstage>
<tab idMso ="TabNew" visible="false"/>
</backstage>
我相信我有所有当前的参考资料,但也许没有:
- Microsoft.Office.Interop.Word(版本 15.0.0.0)
- Microsoft.Office.Tools(版本 10.0.0.0)
- Microsoft.Office.Tools.Word(版本 10.0.0.0)
- Microsoft.Office.Common(版本 10.0.0.0)
- Microsoft.Office.Tools.Word.v4.0.Utilities(版本 10.0.0.0)
这不会禁用文件菜单下的 "Save As" 或我选择的任何其他选项。我错过了什么?有些东西没有正确连接?有些东西过时了?
你没有做错任何事。问题在于后台,因为它是为 Word 2013 及更高版本设计的。无法直接重新调整 Backstage 命令的用途。您显示的代码适用于调用 FileSaveAs 的任何功能区按钮或键盘快捷键,但不适用于 Backstage。
对于后台,您可以使用两种基本方法:
- 隐藏 built-in 后台菜单并在功能区中构建您自己的菜单XML。
- 如果可用,请处理一个事件。在这种情况下,
DocumentBeforeSave
事件可能会有所帮助。
如果您对第一个选项感兴趣,我推荐这两篇 MSDN 文章,它们在一定程度上描述了如何使用功能区中的 Backstage XML:
Introduction to the Office 2010 Backstage View for Developers,
Customizing the Office 2010 Backstage View for Developers. You can download a list of Control IDs for Office 2013: https://www.microsoft.com/en-us/download/details.aspx?id=36798
由于重建“另存为”选项卡的全部内容非常困难,许多人决定使用 DocumentBeforeSave
事件。
我目前正在尝试禁用 Microsoft Word 2016 文件菜单中的几个命令。
目前我有:
Main.cs:
protected override Office.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
return new Ribbon();
}
Ribbon.cs:
public string ReturnUI()
{
return GetSource("Ribbon.xml");
}
private static string GetSource(string sourceName)
{
Assembly asm = Assembly.GetExecutingAssembly();
string[] sourceNames = asm.GetManifestSourceNames();
for (int x = 0; x < sourceNames.Length; ++x)
{
if (string.Compare(sourceName, sourceNames[x], StringComparison.OrdinalIgnoreCase) == 0)
{
using (StreamReader sr = new StreamReader(asm.GetManifestSourceStream(sourceNames[x])))
{
if (sr != null)
{
return sr.ReadToEnd();
}
}
}
}
return "";
}
Ribbon.xml:
<commands>
<command idMso="FileSaveAs" enabled="false" />
</commands>
<backstage>
<tab idMso ="TabNew" visible="false"/>
</backstage>
我相信我有所有当前的参考资料,但也许没有:
- Microsoft.Office.Interop.Word(版本 15.0.0.0)
- Microsoft.Office.Tools(版本 10.0.0.0)
- Microsoft.Office.Tools.Word(版本 10.0.0.0)
- Microsoft.Office.Common(版本 10.0.0.0)
- Microsoft.Office.Tools.Word.v4.0.Utilities(版本 10.0.0.0)
这不会禁用文件菜单下的 "Save As" 或我选择的任何其他选项。我错过了什么?有些东西没有正确连接?有些东西过时了?
你没有做错任何事。问题在于后台,因为它是为 Word 2013 及更高版本设计的。无法直接重新调整 Backstage 命令的用途。您显示的代码适用于调用 FileSaveAs 的任何功能区按钮或键盘快捷键,但不适用于 Backstage。
对于后台,您可以使用两种基本方法:
- 隐藏 built-in 后台菜单并在功能区中构建您自己的菜单XML。
- 如果可用,请处理一个事件。在这种情况下,
DocumentBeforeSave
事件可能会有所帮助。
如果您对第一个选项感兴趣,我推荐这两篇 MSDN 文章,它们在一定程度上描述了如何使用功能区中的 Backstage XML: Introduction to the Office 2010 Backstage View for Developers, Customizing the Office 2010 Backstage View for Developers. You can download a list of Control IDs for Office 2013: https://www.microsoft.com/en-us/download/details.aspx?id=36798
由于重建“另存为”选项卡的全部内容非常困难,许多人决定使用 DocumentBeforeSave
事件。