Hi guys,
i am writing a SharePoint event recivers that fill a word template Mergefield from a SharePoint list filed. When i debug the code i recive this COM exception
"Word was unable to read this document. It may be corrupt.
This is my code:
using System; using System.Security.Permissions; using Microsoft.SharePoint; using Microsoft.SharePoint.Utilities; using Microsoft.SharePoint.Workflow; using Microsoft.Office.Interop.Word; namespace MeetingReportTemplate.ToWordTemplate { /// <summary> /// List Item Events /// </summary> public class ToWordTemplate : SPItemEventReceiver { /// <summary> /// An item was updated. /// </summary> public override void ItemUpdated(SPItemEventProperties properties) { base.ItemUpdated(properties); SPListItem currentItem = properties.ListItem; if (currentItem["Stampa"].ToString() == "Yes") { string ProtocolText = currentItem["Protocol Text"].ToString(); Object oMissing = System.Reflection.Missing.Value; Object oTemplatePath = "C:\\Users\\Administrator\\Desktop\\Protocol2014.dotx"; Application wordApp = new Application(); Document wordDoc = new Document(); wordDoc = wordApp.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing); foreach (Field myMergeField in wordDoc.Fields) { Range rngFieldCode = myMergeField.Code; String fieldText = rngFieldCode.Text; // ONLY GETTING THE MAILMERGE FIELDS if (fieldText.StartsWith(" MERGEFIELD")) { // THE TEXT COMES IN THE FORMAT OF // MERGEFIELD MyFieldName \\* MERGEFORMAT // THIS HAS TO BE EDITED TO GET ONLY THE FIELDNAME "MyFieldName" Int32 endMerge = fieldText.IndexOf("\\"); Int32 fieldNameLength = fieldText.Length - endMerge; String fieldName = fieldText.Substring(11, endMerge - 11); // GIVES THE FIELDNAMES AS THE USER HAD ENTERED IN .dot FILE fieldName = fieldName.Trim(); // **** FIELD REPLACEMENT IMPLEMENTATION GOES HERE ****// // THE PROGRAMMER CAN HAVE HIS OWN IMPLEMENTATIONS HERE if (fieldName == "Text") { myMergeField.Select(); wordApp.Selection.TypeText(ProtocolText); } } } wordDoc.SaveAs("myfile.doc"); wordApp.Documents.Open("myFile.doc"); wordApp.Application.Quit(); } } } }Any suggestions?