Results 1 to 5 of 5

Thread: Word Docs

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Word Docs

    I am trying to:

    1) copy an existing document (text) into a new document (done)
    2) perform a search/replace on the new document (help!)
    3) save the new document

    I can get it to replace etc.. on the existing document, but not the new one. Any ideas on how to do this? Really stuck!

    everytime I try to execute the Content.Find.Execute() method on the new document, it returns false


    Code:
    aDoc.Activate();
    				object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
    				aDoc.Select();
    				aDoc.Content.Copy();
    
    				allDocument.Activate();
    				allDocument.Content.Paste(); //paste into new doc
    
    				Paragraph para;
    				para = allDocument.Content.Paragraphs.Add(ref missing);
    				para.Range.InsertParagraphAfter();
    
    				allDocument.Activate();
    				allDocument.Content.Select();
    				allDocument.Content.Find.ClearFormatting();
    
    				allDocument.Content.Find.Text = "[namehere]";
    				allDocument.Content.Find.Replacement.Text = currentGuest.Lastname + ", " + currentGuest.Firstname;
    
    				bool result = allDocument.Content.Find.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);

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Re: Word Docs

    One method I beleive should work is to save the new document and then reopen it in the same way that you opened the original document.
    » Twitter: @rudi_visser : Website: www.rudiv.se «

    If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Word Docs

    well ive kinda done that except didnt save - it works fine UNTIL we loop again..or go onto the next loop, it doesnt find the text, but it is appending, which is the main thing


    Code:
    ApplicationClass wordApp = new ApplicationClass(); //create an instance of MS Word
    			Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Open(ref fullPathAndfileName, ref missing, ref isFalse, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isTrue, ref missing, ref missing, ref missing, ref missing);
    
    			wordApp.Visible = false;
    
    
    			ApplicationClass anotherWordApp = new ApplicationClass();
    			Microsoft.Office.Interop.Word.Document anotherADoc = anotherWordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
    			anotherWordApp.Visible = false;
    
    
    			aDoc.Select();
    			aDoc.Content.Copy();
    
    			foreach (UserEntity currentGuest in collectionOfGuests)
    			{
    				anotherWordApp.Selection.Find.ClearFormatting();
    				anotherWordApp.Selection.Find.Text = "[namehere]";
    
    				anotherWordApp.Selection.Find.Replacement.ClearFormatting();
    				anotherWordApp.Selection.Find.Replacement.Text = currentGuest.Lastname + ", " + currentGuest.Firstname;
    
    				bool result = anotherWordApp.Selection.Find.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);
    
    				if (result)
    				{
    					anotherWordApp.Selection.InsertNewPage();
    					anotherWordApp.Selection.Paste();
    					anotherWordApp.Selection.Select();
    				}
    			}

    so next time round, it appears to append the document (good) so we have the "template" text as well as the replaced text which was done in the previous loop however next time we execute the find...it fails to find the text (even though it exists)

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Word Docs

    got it sorted now

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  5. #5
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Re: Word Docs

    Glad to hear it.
    » Twitter: @rudi_visser : Website: www.rudiv.se «

    If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width