-
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);
-
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.
-
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)
-
Re: Word Docs
-
Re: Word Docs
Glad to hear it. :) :wave: