none
error using WordApp.Selection.Find.Execute (...) in word document RRS feed

  • Question

  • i written bellow code using console application in visual studio 2012

    WordApp.Selection.Find.Execute show an error message(This command is not available)

    any body help its Great!

     private void button2_Click(object sender, EventArgs e)
            {
                //  create offer letter
                try
                {
                    //  Just to kill WINWORD.EXE if it is running
                    //killprocess("winword");
                    //  copy letter format to temp.doc
                    File.Copy("D:\\MyTest.doc", "c:\\temp.doc", true);
                    //  create missing object
                    object missing = System.Reflection.Missing.Value;
                    //  create Word application object
                    Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
                    //  create Word document object
                    Microsoft.Office.Interop.Word.Document aDoc = null;
                    //  create & define filename object with temp.doc
                    object filename = "c:\\temp.doc";
                    //  if temp.doc available
                    if (File.Exists((string)filename))
                    {
                        object readOnly = false;
                        object isVisible = false;
                        object lbldate = "<Insert OtherParty1>";
                        object txtdate = "PartName1";
                        //  make visible Word application
                        wordApp.Visible = false;
                        //  open Word document named temp.doc
                        aDoc = wordApp.Documents.Open(ref filename, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                           ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
                        aDoc.Activate();                  

                        //  Call FindAndReplace()function for each change
                        FindAndReplace(wordApp, lbldate, txtdate);                  

                        aDoc.Save();
                    }
                    else
                        MessageBox.Show("File does not exist.", "No File", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                catch (Exception)
                {
                    MessageBox.Show("Error in process.", "Internal Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            private void FindAndReplace(Microsoft.Office.Interop.Word.Application WordApp, object findText, object replaceWithText)
            {
                object matchCase = true;
                object matchWholeWord = true;
                object matchWildCards = false;
                object matchSoundsLike = false;
                object nmatchAllWordForms = false;
                object forward = true;
                object format = false;
                object matchKashida = false;
                object matchDiacritics = false;
                object matchAlefHamza = false;
                object matchControl = false;
                object read_only = false;
                object visible = true;
                object replace = 2;
                object wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
                object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;          

                WordApp.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord, ref matchWildCards, ref matchSoundsLike,
                ref nmatchAllWordForms, ref forward,
                ref wrap, ref format, ref replaceWithText,
                ref replaceAll, ref matchKashida,
                ref matchDiacritics, ref matchAlefHamza,
                ref matchControl);
            }

                       
    Thursday, June 26, 2014 7:22 AM

Answers

  • Hello Venkat,

    First of all, I'd recommend releasing underlying COM objects instantly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Word object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. You can read more about this in the Systematically Releasing Objects article in MSDN. It describes Outlook, but the same can be applied to other Office applications (Word in your case).

    Where do you run the code listed above? Is suspect that it belongs to a VSTO add-in due to the fact that you posted it to the VSTO related forum. If so, you shouldn't create a new instance of the Word Application in the code:

     Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();

    Instead, you need to use the Application property provided by the VSTO runtime (see Globals.ThisAddin.Application).

    You are referring to the following line of code:

    WordApp.Selection.Find.Execute 

    What property or method call exactly fires the exception?

    I'd recommend breaking the chain of calls and declare each property and method call on a separate line. Thus, you will be able to identify the cause exactly.

    Did you try to use the Find method of the Range class instead? Does it work as expected?

    Thursday, June 26, 2014 12:21 PM
  • Hi Venkat

    I recommend as the first trouble-shooting step that you make both the Word.Application and the Word.Document VISIBLE. Some things in Word depend on the page actually being laid out on the screen, especially if you work with the SELECTION object.

    The next thing to check is WHERE the Selection is when the error is thrown. There are states where Word is not able to execute a command, and that appears to be what the error message is telling you. By default, the Selection will be at the first character in the document body. Perhaps there's a graphic anchored at the very first character? That can cause odd behavior, for example.

    It is possible that using Range.Find.Execute, as Eugene suggests, will help if the issue is the location of the Selection. In that case, you'd want to define the Range as: Word.Range rngFind = aDoc.Content; and pass that to your method instead of the Word.Application object. But it's important to note that using Range.Find.Execute will only work in the specified Range and no other Document StoryRange, such as Headers and Footers. Each Range needs to be addressed separately and specifically.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, June 26, 2014 6:26 PM
    Moderator

All replies

  • Hello Venkat,

    First of all, I'd recommend releasing underlying COM objects instantly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Word object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. You can read more about this in the Systematically Releasing Objects article in MSDN. It describes Outlook, but the same can be applied to other Office applications (Word in your case).

    Where do you run the code listed above? Is suspect that it belongs to a VSTO add-in due to the fact that you posted it to the VSTO related forum. If so, you shouldn't create a new instance of the Word Application in the code:

     Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();

    Instead, you need to use the Application property provided by the VSTO runtime (see Globals.ThisAddin.Application).

    You are referring to the following line of code:

    WordApp.Selection.Find.Execute 

    What property or method call exactly fires the exception?

    I'd recommend breaking the chain of calls and declare each property and method call on a separate line. Thus, you will be able to identify the cause exactly.

    Did you try to use the Find method of the Range class instead? Does it work as expected?

    Thursday, June 26, 2014 12:21 PM
  • Hi Venkat

    I recommend as the first trouble-shooting step that you make both the Word.Application and the Word.Document VISIBLE. Some things in Word depend on the page actually being laid out on the screen, especially if you work with the SELECTION object.

    The next thing to check is WHERE the Selection is when the error is thrown. There are states where Word is not able to execute a command, and that appears to be what the error message is telling you. By default, the Selection will be at the first character in the document body. Perhaps there's a graphic anchored at the very first character? That can cause odd behavior, for example.

    It is possible that using Range.Find.Execute, as Eugene suggests, will help if the issue is the location of the Selection. In that case, you'd want to define the Range as: Word.Range rngFind = aDoc.Content; and pass that to your method instead of the Word.Application object. But it's important to note that using Range.Find.Execute will only work in the specified Range and no other Document StoryRange, such as Headers and Footers. Each Range needs to be addressed separately and specifically.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, June 26, 2014 6:26 PM
    Moderator