创建了 Word 2007 应用程序级单词加载项 - 不适用于新文档
我按照此处文章创建了一个 Word 加载项。我按 F5 并运行该项目,它按预期工作,我认为该加载项已安装在我的计算机中。因此,现在我打开另一个 Word 2007 实例并创建一个文档,但我没有看到该代码在新文档上运行。我错过了什么吗?
以下是我正在使用的代码:-
using Word = Microsoft.Office.Interop.Word;
namespace WordAddIn1
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.DocumentBeforeSave +=
new Word.ApplicationEvents4_DocumentBeforeSaveEventHandler(Application_DocumentBeforeSave);
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
void Application_DocumentBeforeSave(Word.Document Doc, ref bool SaveAsUI, ref bool Cancel)
{
Doc.Paragraphs[1].Range.InsertParagraphBefore();
Doc.Paragraphs[1].Range.Text = "Text was added by using code.---";
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}
来自此处:- 完成项目开发后,请从开发计算机中删除加载项程序集、注册表项和安全设置。否则,每次您在开发计算机上打开 Word 时,加载项都会继续运行。 清理开发计算机上已完成的项目 在 Visual Studio 中的“生成”菜单上,单击“清理解决方案”。
现在,当我不清理解决方案时,我应该始终拥有 Word 2007 的加载项,对吧?我根本不认为这种情况会发生。
I have created a Word add-in following the article here. I press F5 and run the project and It works as expected and I would think that the add-in has been installed in my machine. So, now I open another instance of Word 2007 and create a document and I dont see that code working on the new document. Am I missing something?
Following is the code I am using :-
using Word = Microsoft.Office.Interop.Word;
namespace WordAddIn1
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.DocumentBeforeSave +=
new Word.ApplicationEvents4_DocumentBeforeSaveEventHandler(Application_DocumentBeforeSave);
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
void Application_DocumentBeforeSave(Word.Document Doc, ref bool SaveAsUI, ref bool Cancel)
{
Doc.Paragraphs[1].Range.InsertParagraphBefore();
Doc.Paragraphs[1].Range.Text = "Text was added by using code.---";
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}
From here:-
When you finish developing a project, remove the add-in assembly, registry entries, and security settings from your development computer. Otherwise, the add-in will continue to run every time that you open Word on your development computer.
To clean up the completed project on your development computer
In Visual Studio, on the Build menu, click Clean Solution.
Now, when I dont clean the solution, I should have the add-in for the Word 2007 all the time, right? I don't see that happening at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您做事的方式,有几种可能性。
最有可能的是,当您打开新实例时,调试器未附加,因此您的断点不会被命中。
另一个可能的原因是,它不是新实例,而是同一实例中的新文档,并且两个文档之间共享同一加载项的同一实例。在这种情况下,ThisAdd.Loaded 事件将不会再次被触发,您必须侦听正在激活的新文档(从内存中单词没有 NewDocument 事件)
什么样的代码不起作用?是看不到效果,还是断点没有命中?
Depending on the way you are doing things there are a few possibilities.
Most likely is that when you open the new instance, the debugger isn't attached, so your breakpoints are not being hit.
Another possible reason is that it is not a new instance but actually a new document in the same instance, and the the same instance of the same add-in is shared between the two documents. In that case ThisAdd.Loaded event will not be fired again, you have to listen for new documents being activated (from memory word doesn't have a NewDocument event)
What sort of code is not working? Is it the effects that cannot be seen, or a breakpoint not being hit?
我发现从 Word 2007 中删除该加载项可以解决该问题。如果您遇到此问题,请尝试从 Word 2007 中删除该加载项并再次构建解决方案,您的更改就会生效。由于某种原因,“干净的解决方案”有时不会删除单词加载项(至少有时在我的情况下:-))
I see that removing that add-in from word 2007 resolves the problem. If you face this problem, try removing the add-in from Word 2007 and build the solution the solution again and your change will take effect. For some reason "Clean solution" does not remove the word add-in sometimes (atleast sometimes in my case :-))