Nick Donegan
Nick Donegan

Reputation: 5

Unable to Utilize Application.Select.Find in a Word 2013 VSTO Plug-in

I'm a rank novice at C#, and am currently using Visual Studio 2015 to try and create a Ribbon-based VSTO Plug-in for Word 2013 that accomplishes the following tasks upon a button click:

I am currently able to perform the first and last tasks fine. Ironically enough, I have used code directly from the MSDN article on Find & Replace, but keep encountering an error preventing my build. I have tried a number of solutions, including replacing Application.Selection.Find with Word.Selection.Find, and WordApp.Selection.Find, but to no avail.

My exact error is as follows: " Error CS0117 'Application' does not contain a definition for 'Selection'"

I feel so close to victory here it's driving me batty. I've posted my full code below.

Thank you very much in advance for any help and/or insight provided!

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); }
        }
    }
}

Upvotes: 0

Views: 636

Answers (2)

Cindy Meister
Cindy Meister

Reputation: 25693

Nick's analysis is correct, but the suggested solution is not optimal for a VSTO add-in.

Since this is a VSTO add-in, using GetObject to get the instance of the Word.Application is not the correct approach. The Word.Application is available directly through any VSTO add-in, since the add-in is running in-process with the Word.Application.

In the ThisAddIn.cs class, usually in the ThisAddIn_Startup event:

Word.Application wdApp = this.Application;

If you want/need to access it from a different class, then either declare the Word.Application object as a class member and assign it in ThisAddIn_Startup

OR access it through the Globals object, which gives your code access to all VSTO objects across all classes:

Word.Application app = Globals.ThisAddIn.Application;

Upvotes: 1

Nick Otten
Nick Otten

Reputation: 712

Follow up on the comments below the original question, When looking over the code to make the example I noticed you where using the wrong application object. Instead of asking Ms Word to find your selection you where asking it to your C# application.

I don't have Ms Word installed on my computer but out of memory I'd say you should change

Word.Find findObject = Application.Selection.Find;

to

var app = (Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
Word.Find findObject = app.Selection.Find;

Keep in mind that this solution expects that Ms Word is up and running. If word is not running it would result in a exception.

Upvotes: 0

Related Questions