Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Office Automation - problem with Find object

Office Automation - problem with Find object

Scheduled Pinned Locked Moved C#
debuggingcsharpcomtesting
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Matt Casto
    wrote on last edited by
    #1

    I'm trying to get office automation working, and most things work except working with the Find object. For some reason, every time I try to access the Find object (both through Selection or Range) I find that the Find object is a null reference. If I try to add a watch to my Document and debug, when I expand the Selection then the Find object, my debugger crashes. I am running Office 2000. Trying similar code in VB.NET yields similar results. The following is a simple Console app that I created to test this. It only requires that the "C:\TestFindReplace\TestDoc.doc" is a valid path with a word doc that contains the text ":FULLNAME:" somewhere. Of course, you can change the filename and path to whatever if you want to test this code. Also, you will need to add a reference to the Microsoft Word 9.0 COM object. [STAThread] static void Main(string[] args) {   Console.Write("Enter the person's name: ");   string fullName = Console.ReadLine();   Console.WriteLine();   FindReplace(fullName); } static private void FindReplace(string fullName) {   object oFileName = @"C:\TestFindReplace\TestDoc.doc";   object oNewFileName = @"C:\TestFindReplace\" + fullName + ".doc";   object oFalse = false;   object oMissing = Type.Missing;   object oReplaceAll = Word.WdReplace.wdReplaceAll;   Word.Application WordApp = new Word.ApplicationClass();   Word.Document myDoc = WordApp.Documents.Open(ref oFileName,     ref oMissing, ref oFalse, ref oMissing, ref oMissing,     ref oMissing, ref oMissing, ref oMissing, ref oMissing,     ref oMissing, ref oMissing, ref oFalse);   try   {     myDoc.Application.Selection.Find.Text = ":FULLNAME:";     myDoc.Application.Selection.Find.Replacement.Text = fullName;     myDoc.Application.Selection.Find.Execute(ref oMissing,       ref oMissing, ref oMissing, ref oMissing,       ref oMissing, ref oMissing, ref oMissing,       ref oMissing, ref oMissing, ref oMissing,       ref oReplaceAll, ref oMissing, ref oMissing,       ref oMissing, ref oMissing);     myDoc.SaveAs(ref oNewFileName, ref oMissing, ref oMissing, ref oMissing,   &nb

    D 1 Reply Last reply
    0
    • M Matt Casto

      I'm trying to get office automation working, and most things work except working with the Find object. For some reason, every time I try to access the Find object (both through Selection or Range) I find that the Find object is a null reference. If I try to add a watch to my Document and debug, when I expand the Selection then the Find object, my debugger crashes. I am running Office 2000. Trying similar code in VB.NET yields similar results. The following is a simple Console app that I created to test this. It only requires that the "C:\TestFindReplace\TestDoc.doc" is a valid path with a word doc that contains the text ":FULLNAME:" somewhere. Of course, you can change the filename and path to whatever if you want to test this code. Also, you will need to add a reference to the Microsoft Word 9.0 COM object. [STAThread] static void Main(string[] args) {   Console.Write("Enter the person's name: ");   string fullName = Console.ReadLine();   Console.WriteLine();   FindReplace(fullName); } static private void FindReplace(string fullName) {   object oFileName = @"C:\TestFindReplace\TestDoc.doc";   object oNewFileName = @"C:\TestFindReplace\" + fullName + ".doc";   object oFalse = false;   object oMissing = Type.Missing;   object oReplaceAll = Word.WdReplace.wdReplaceAll;   Word.Application WordApp = new Word.ApplicationClass();   Word.Document myDoc = WordApp.Documents.Open(ref oFileName,     ref oMissing, ref oFalse, ref oMissing, ref oMissing,     ref oMissing, ref oMissing, ref oMissing, ref oMissing,     ref oMissing, ref oMissing, ref oFalse);   try   {     myDoc.Application.Selection.Find.Text = ":FULLNAME:";     myDoc.Application.Selection.Find.Replacement.Text = fullName;     myDoc.Application.Selection.Find.Execute(ref oMissing,       ref oMissing, ref oMissing, ref oMissing,       ref oMissing, ref oMissing, ref oMissing,       ref oMissing, ref oMissing, ref oMissing,       ref oReplaceAll, ref oMissing, ref oMissing,       ref oMissing, ref oMissing);     myDoc.SaveAs(ref oNewFileName, ref oMissing, ref oMissing, ref oMissing,   &nb

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      That's because the Find object isn't part of a Document object (at leat in Word 2003). It should look more like this:

      object oFileName = @"C:\TestFindReplace\TestDoc.doc";
      object oNewFileName = @"C:\TestFindReplace\" + fullName + ".doc";
      object oFalse = false;
      object oMissing = Type.Missing;
      object oReplaceAll = Word.WdReplace.wdReplaceAll;

      Word.Application WordApp = New Word.ApplicationClass();
      Word.Document myDoc = WordApp.Documents.Open(ref oFileName,
      ref oMissing, ref oFalse, ref oMissing, ref oMissing,
      ref oMissing, ref oMissing, ref oMissing, ref oMissing,
      ref oMissing, ref oMissing, ref oFalse);
      try
      {
      Word.Find fnd = WordApp.Selection.Find;
      fnd.Text = ":FULLNAME:";
      fnd.Replacement.Text = fullName;

      fnd.Execute(ref oMissing,
      ref oMissing, ref oMissing, ref oMissing,
      ref oMissing, ref oMissing, ref oMissing,
      ref oMissing, ref oMissing, ref oMissing,
      ref oReplaceAll, ref oMissing, ref oMissing,
      ref oMissing, ref oMissing);

      RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      M 1 Reply Last reply
      0
      • D Dave Kreskowiak

        That's because the Find object isn't part of a Document object (at leat in Word 2003). It should look more like this:

        object oFileName = @"C:\TestFindReplace\TestDoc.doc";
        object oNewFileName = @"C:\TestFindReplace\" + fullName + ".doc";
        object oFalse = false;
        object oMissing = Type.Missing;
        object oReplaceAll = Word.WdReplace.wdReplaceAll;

        Word.Application WordApp = New Word.ApplicationClass();
        Word.Document myDoc = WordApp.Documents.Open(ref oFileName,
        ref oMissing, ref oFalse, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oFalse);
        try
        {
        Word.Find fnd = WordApp.Selection.Find;
        fnd.Text = ":FULLNAME:";
        fnd.Replacement.Text = fullName;

        fnd.Execute(ref oMissing,
        ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing,
        ref oReplaceAll, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing);

        RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        M Offline
        M Offline
        Matt Casto
        wrote on last edited by
        #3

        Unfortunately, I get the same error. I'm using Office 2000 and I suspect that it might be a problem with this version of office. I had a co-worker with the same PC configuration run this app and he got the same error. I'm leaving for two weeks of vacation tomorrow, so I'll have to put this issue on hold until I'm back. Thanks for your help!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups