UserControl 不包含 Application 的定义
UserControl does not contain a defination for Application
我正在尝试通过单击按钮在活动的 Word 文档中执行搜索操作。
在我的代码中出现错误是
按钮位于自定义任务窗格上
Error CS1061 'UserControl1' does not contain a definition for 'Application' and no accessible extension method 'Application' accepting a first argument of type 'UserControl1' could be found (are you missing a using directive or an assembly reference?) WordAddIn1 c:\users\veroot\source\repos\WordAddIn1\WordAddIn1\UserControl1.cs 29 Active
密码是
private void button1_Click(object sender, EventArgs e)
{
object findText = textBox1.Text;
object missing = System.Type.Missing;
Word.Document document = this.Application.ActiveDocument;
Word.Range rng = document.Range(0, Type.Missing);
rng.Find.Highlight = 0;
rng.Find.Forward = true;
do
{
if (rng.HighlightColorIndex == WdColorIndex.wdYellow)
{
rng.HighlightColorIndex = WdColorIndex.wdRed;
rng.Font.ColorIndex = WdColorIndex.wdBlue;
}
int intPosition = rng.End;
rng.Start = intPosition;
} while (rng.Find.Execute("", missing, missing, missing, missing, missing, true,
missing, missing, missing, missing, missing, missing, missing, missing));
}
在 VSTO 解决方案中,只能使用关键字 this
来引用 ThisAddin
class 中的宿主 Office 应用程序。在所有其他 classes 中,包括 UserControl 的 es,this
将引用 class(UserControl)并且与宿主 Office 应用程序没有关系或连接。因此,对于问题中显示的代码,this
指的是 UserControl class.
为了引用 VSTO 加载项是 运行 的 Office 应用程序,最好使用 Globals
关键字。例如
Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
或
Word.Document doc = Globals.ThisAddIn.app.ActiveDocument;
其中 app
是 ThisAddin
class 中的 class 级字段 - 示例声明:
public partial class ThisAddIn
{
public Word.Application app;
在 ThisAddin_Startup
中分配 - 例如:
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
app = this.Application;
我正在尝试通过单击按钮在活动的 Word 文档中执行搜索操作。 在我的代码中出现错误是
按钮位于自定义任务窗格上
Error CS1061 'UserControl1' does not contain a definition for 'Application' and no accessible extension method 'Application' accepting a first argument of type 'UserControl1' could be found (are you missing a using directive or an assembly reference?) WordAddIn1 c:\users\veroot\source\repos\WordAddIn1\WordAddIn1\UserControl1.cs 29 Active
密码是
private void button1_Click(object sender, EventArgs e)
{
object findText = textBox1.Text;
object missing = System.Type.Missing;
Word.Document document = this.Application.ActiveDocument;
Word.Range rng = document.Range(0, Type.Missing);
rng.Find.Highlight = 0;
rng.Find.Forward = true;
do
{
if (rng.HighlightColorIndex == WdColorIndex.wdYellow)
{
rng.HighlightColorIndex = WdColorIndex.wdRed;
rng.Font.ColorIndex = WdColorIndex.wdBlue;
}
int intPosition = rng.End;
rng.Start = intPosition;
} while (rng.Find.Execute("", missing, missing, missing, missing, missing, true,
missing, missing, missing, missing, missing, missing, missing, missing));
}
在 VSTO 解决方案中,只能使用关键字 this
来引用 ThisAddin
class 中的宿主 Office 应用程序。在所有其他 classes 中,包括 UserControl 的 es,this
将引用 class(UserControl)并且与宿主 Office 应用程序没有关系或连接。因此,对于问题中显示的代码,this
指的是 UserControl class.
为了引用 VSTO 加载项是 运行 的 Office 应用程序,最好使用 Globals
关键字。例如
Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
或
Word.Document doc = Globals.ThisAddIn.app.ActiveDocument;
其中 app
是 ThisAddin
class 中的 class 级字段 - 示例声明:
public partial class ThisAddIn
{
public Word.Application app;
在 ThisAddin_Startup
中分配 - 例如:
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
app = this.Application;