无法在 Word 2013 VSTO 插件中使用 Application.Select.Find
Unable to Utilize Application.Select.Find in a Word 2013 VSTO Plug-in
我是 C# 的初级新手,目前正在使用 Visual Studio 2015 尝试为 Word 2013 创建基于功能区的 VSTO 插件,单击按钮即可完成以下任务:
- 在文本框中接受用户的输入。
- 查找一系列字符 ("xxxxx") 并使用先前的用户输入替换整个文档中的字符集。
- 将文档另存为 PDF。
我目前能够很好地执行 first 和 last 任务。具有讽刺意味的是,我直接使用了 MSDN article on Find & Replace 中的代码,但一直遇到阻止我构建的错误。我尝试了多种解决方案,包括将 Application.Selection.Find
替换为 Word.Selection.Find
和 WordApp.Selection.Find
,但无济于事。
我的确切错误如下:“
错误 CS0117 'Application' 不包含 'Selection'"
的定义
我觉得这里离胜利如此之近,这让我抓狂。我已经在下面发布了我的完整代码。
非常感谢您提供的任何帮助 and/or 见解!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Tools.Word;
namespace RestRef_Automator_Test
{
public partial class Ribbon1
{
private bool flag;
private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
{
}
private void button1_Click(object sender, RibbonControlEventArgs e)
{
//Get, Find, & Replace RID
int myRID = 0;
string myString = myRID.ToString();
flag = int.TryParse(RID.Text, out myRID);
if (flag == false)
{
MessageBox.Show("Please enter in a number.", "Input Error");
return;
}
Word.Find findObject = Application.Selection.Find;
findObject.ClearFormatting();
findObject.Text = "xxxxx";
findObject.Replacement.ClearFormatting();
findObject.Replacement.Text = myString;
object replaceALL = Word.WdReplace.wdReplaceAll;
object missing = null;
findObject.Execute(
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref replaceALL, ref missing, ref missing, ref missing, ref missing);
//Save as PDF w/ applicable name.
SaveFileDialog dlg = new SaveFileDialog();
dlg.FileName = "*";
dlg.DefaultExt = "pdf";
dlg.ValidateNames = true;
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{ Globals.ThisAddIn.Application.ActiveDocument.ExportAsFixedFormat(dlg.FileName, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF, OpenAfterExport: true); }
}
}
}
跟进原始问题下方的评论,在查看示例代码时,我注意到您使用了错误的应用程序对象。而不是让 Word 女士找到您的选择,而是向您的 C# 应用程序询问。
我的电脑上没有安装 Ms Word,但内存不足我会说你应该更改
Word.Find findObject = Application.Selection.Find;
至
var app = (Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
Word.Find findObject = app.Selection.Find;
请记住,此解决方案需要 Ms Word 启动并且 运行。如果单词不是 运行,则会导致异常。
Nick 的分析是正确的,但建议的解决方案对于 VSTO 外接程序来说并不是最佳的。
由于这是一个 VSTO 加载项,使用 GetObject
获取 Word.Application 的实例 不是 正确的方法。 Word.Application 可直接通过任何 VSTO 加载项使用,因为该加载项 运行 在 Word.Application.
进程中
在ThisAddIn.cs
class,通常在ThisAddIn_Startup
事件中:
Word.Application wdApp = this.Application;
如果您 want/need 从不同的 class 访问它,则要么将 Word.Application 对象声明为 class 成员并将其分配给 ThisAddIn_Startup
或通过 Globals
对象访问它,这使您的代码可以访问所有 classes 中的所有 VSTO 对象:
Word.Application app = Globals.ThisAddIn.Application;
我是 C# 的初级新手,目前正在使用 Visual Studio 2015 尝试为 Word 2013 创建基于功能区的 VSTO 插件,单击按钮即可完成以下任务:
- 在文本框中接受用户的输入。
- 查找一系列字符 ("xxxxx") 并使用先前的用户输入替换整个文档中的字符集。
- 将文档另存为 PDF。
我目前能够很好地执行 first 和 last 任务。具有讽刺意味的是,我直接使用了 MSDN article on Find & Replace 中的代码,但一直遇到阻止我构建的错误。我尝试了多种解决方案,包括将 Application.Selection.Find
替换为 Word.Selection.Find
和 WordApp.Selection.Find
,但无济于事。
我的确切错误如下:“ 错误 CS0117 'Application' 不包含 'Selection'"
的定义我觉得这里离胜利如此之近,这让我抓狂。我已经在下面发布了我的完整代码。
非常感谢您提供的任何帮助 and/or 见解!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Tools.Word;
namespace RestRef_Automator_Test
{
public partial class Ribbon1
{
private bool flag;
private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
{
}
private void button1_Click(object sender, RibbonControlEventArgs e)
{
//Get, Find, & Replace RID
int myRID = 0;
string myString = myRID.ToString();
flag = int.TryParse(RID.Text, out myRID);
if (flag == false)
{
MessageBox.Show("Please enter in a number.", "Input Error");
return;
}
Word.Find findObject = Application.Selection.Find;
findObject.ClearFormatting();
findObject.Text = "xxxxx";
findObject.Replacement.ClearFormatting();
findObject.Replacement.Text = myString;
object replaceALL = Word.WdReplace.wdReplaceAll;
object missing = null;
findObject.Execute(
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref replaceALL, ref missing, ref missing, ref missing, ref missing);
//Save as PDF w/ applicable name.
SaveFileDialog dlg = new SaveFileDialog();
dlg.FileName = "*";
dlg.DefaultExt = "pdf";
dlg.ValidateNames = true;
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{ Globals.ThisAddIn.Application.ActiveDocument.ExportAsFixedFormat(dlg.FileName, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF, OpenAfterExport: true); }
}
}
}
跟进原始问题下方的评论,在查看示例代码时,我注意到您使用了错误的应用程序对象。而不是让 Word 女士找到您的选择,而是向您的 C# 应用程序询问。
我的电脑上没有安装 Ms Word,但内存不足我会说你应该更改
Word.Find findObject = Application.Selection.Find;
至
var app = (Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
Word.Find findObject = app.Selection.Find;
请记住,此解决方案需要 Ms Word 启动并且 运行。如果单词不是 运行,则会导致异常。
Nick 的分析是正确的,但建议的解决方案对于 VSTO 外接程序来说并不是最佳的。
由于这是一个 VSTO 加载项,使用 GetObject
获取 Word.Application 的实例 不是 正确的方法。 Word.Application 可直接通过任何 VSTO 加载项使用,因为该加载项 运行 在 Word.Application.
在ThisAddIn.cs
class,通常在ThisAddIn_Startup
事件中:
Word.Application wdApp = this.Application;
如果您 want/need 从不同的 class 访问它,则要么将 Word.Application 对象声明为 class 成员并将其分配给 ThisAddIn_Startup
或通过 Globals
对象访问它,这使您的代码可以访问所有 classes 中的所有 VSTO 对象:
Word.Application app = Globals.ThisAddIn.Application;