Using Word i nC#
-
I've tried to used this article http://www.codeproject.com/csharp/CsAutomateWord.asp?target=Word to create a character sheet. I have a CCharacter class, that contains all information about a character, and I want to generate a MS Word document with these information. So I've created .dot document, with thinks like Name _Name_ Age _Age_ in arrays (I've tried also without the array, directly in the text). _XXX_ is code that I will use for replacement. Now, to the code: // Create a Word application Word.ApplicationClass vk_word_app = new Word.ApplicationClass(); // Open the .dot object FileName =Directory.GetCurrentDirectory()+"\\Models\\Sheet.dot"; Word.Document vk_my_doc = vk_word_app.Documents.Open(... // Create a new document Word.Document vk_new_doc = vk_word_app.Documents.Add((... // Copy the .dot into the new document vk_my_doc.Select(); vk_word_app.Selection.Copy(); vk_new_doc.Select(); vk_word_app.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault); // close the .dot vk_my_doc.Close( ref vk_false, ref vk_missing, ref vk_missing ); All the previous steps work. I get a new document, build from my template. Now, I'd like to fill it with actual information. So I do this // Select the new doc vk_new_doc.Select(); // Define some variable object vk_false = false; object vk_true = true; object vk_num = Word.WdFindWrap.wdFindStop; object vk_find = "_Nom_"; object vk_replaceWith = Name_Familly; // This one is a member of CCharacter class object vk_replace = Word.WdReplace.wdReplaceOne; // Try to replace the text vk_word_app.Selection.Find.Execute( ref vk_find, ref vk_false, ref vk_false, ref vk_false, ref vk_false, ref vk_false, ref vk_true, ref vk_num, ref vk_false, ref vk_replaceWith, ref vk_replace, ref vk_false, ref vk_false, ref vk_false, ref vk_false ); And... it doesn't work. I got this error: System.Runtime.InteropServices.COMException (0x800706F7): Le relais a reçu des données incorrectes. at Word.Find.Execute(Object& FindText, Object& MatchCase, Object& MatchWholeWord, Object& MatchWildcards, Object& MatchSoundsLike, Object& MatchAllWordForms, Object& Forward, Object& Wrap, Object& Format, Object& ReplaceWith, Object& Replace, Object& MatchKashida, Object& MatchDiacritics, Object& MatchAlefHamza, Object& MatchControl) My form close, the word doc stays opened. "Le relais a reçu des données incorrectes" would translate as "incorrect data received". Any idea of a way to solve this would