|
-
Apr 10th, 2007, 07:49 AM
#1
Thread Starter
PowerPoster
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);
-
Apr 10th, 2007, 08:19 AM
#2
Hyperactive Member
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.
-
Apr 10th, 2007, 08:47 AM
#3
Thread Starter
PowerPoster
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)
-
Apr 10th, 2007, 10:52 AM
#4
Thread Starter
PowerPoster
Re: Word Docs
got it sorted now
-
Apr 10th, 2007, 10:57 AM
#5
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|