运行加载项时出错 [英] Getting an error when the add-in is run

查看:159
本文介绍了运行加载项时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户按下空格后立即复制用户输入的单词(就像单词编辑器中的拼写检查程序一样)。

我正在使用以下代码:



 private void ThisAddIn_Startup(object sender,System.EventArgs e)
{
ConsoleKeyInfo info = Console.ReadKey();
if(info.KeyChar =='')
{
Word.Range rng = this.Application.ActiveDocument.Words.Last;
rng.Select();
rng.Copy();
String input = Clipboard.GetText(TextDataFormat.Text);
}
}





我收到以下错误:

An mscorlib.dll中出现'System.InvalidOperationException'类型的异常,但未在用户代码中处理。

代表以下行:

 ConsoleKeyInfo info = Console.ReadKey(); 





请帮忙。

解决方案

您需要知道MS Word插件与控制台应用程序 [ ^ ]。



如果要与MS Word交互,则需要使用其对象,方法和事件。



根据我的理解你的要求,你需要使用MS word / document事件之一(我不确定哪一个,如果有的话)。请参阅:

应用程序事件 [ ^ ]

文档事件 [ ^ ]

将事件与文档对象一起使用 [ ^ ]

编写应用程序事件过程 [ ^ ]





我想你正在寻找 KeyPress活动 [< a href =http://office.microsoft.com/en-us/word-help/HV080607445.aspxtarget =_ blanktitle =New Window> ^ ]

但我需要警告你:没有简单的方法来实现这一目标。请参阅 [ ^ ]

Cindy Meister,VSTO / Word MVP,关于VSTO微软论坛,2011年重新Word 2010:

更确切地说,我说Word API或VSTO中没有任何内容可以拿起击键。没有任何内置功能可以在VSTO项目中使用来获取击键。 

您需要使用VBA(不属于VSTO)或者您需要使用Windows API - 这两者都不是VSTO的组成部分。

在Word中,击键只能映射到VBA宏,而不能映射到其他代码。

[ ^ ]。



我会调查你是否可以将GlobalHook应用于Word应用程序,但是,我不知道这是否可以在加载项的上下文中使用。


I want to copy the word entered by a user as soon as the user presses space(Just like the spell checker in word editors do).
I'm using the following code:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            ConsoleKeyInfo info = Console.ReadKey();
            if (info.KeyChar == ' ')
            {
                Word.Range rng = this.Application.ActiveDocument.Words.Last;
                rng.Select();
                rng.Copy();
                String input = Clipboard.GetText(TextDataFormat.Text);
            }
        }



I'm getting the following error:
An exception of type 'System.InvalidOperationException' occurred in mscorlib.dll but was not handled in user code.
for the following line:

ConsoleKeyInfo info = Console.ReadKey();



Please help.

解决方案

You need to know that MS Word addin does not corresponds with Console aplication[^].

If you want to interact with MS Word, you need to use its objects, methods and events.

As per i understand your requirements, you need to use one of MS word/document event (i'm not sure which one, if any). Please, see:
Application Events[^]
Document Events[^]
Using Events with the Document Object[^]
Writing application event procedures[^]

[EDIT]
I think you're looking for KeyPress event[^]
But i need to warn you: there isn't simple way to achieve that. Please see this[^]


Cindy Meister, VSTO/Word MVP, on VSTO Microsoft forum, in 2011 re Word 2010:

More exactly, I said there's nothing in the Word API or VSTO that can pick up keystrokes. There's nothing built-in that you can use in a VSTO project to pick up the keystrokes.

You'd either need to use VBA (which isn't part of VSTO) or you'd need to use the Windows API - both of which are not an integral part of VSTO.

In Word, keystrokes can only be mapped to VBA macros, not to other code.

[^].

I would investigate if you can apply a GlobalHook to the Word Application, but, I have no idea is this is possible within the context of an Add-In.


这篇关于运行加载项时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆