Hi every one i working on a project and i needed to create word template with tags,
i created methods and every thing it work fine but when i get project on my laptap it appear with
this error
Attempted to read or write protected memory. '~/file/xxx.docx' This is often an indication that other memory is corrupt. in asp.net working with files
public static bool WriteInfoToWordDoc(object fileName, Dictionary<string, object> data)
{
bool result = false;
object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document wordDoc = null;
object readOnly = false;
object isVisible = false;
try
{
wordApp.Visible = false;
wordDoc = wordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing,
ref missing, ref missing);
wordDoc.Activate();
foreach (string key in data.Keys)
{
WriteData(wordApp, key, data[key]);
}
wordDoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing);
wordDoc.Close(ref missing, ref missing, ref missing);
result = true;
}
catch (Exception ex)
{
}
return result;
}
private static bool WriteData(Microsoft.Office.Interop.Word.Application wordApp, string key, object value)
{
bool bResult = false;
if (value != null)
{
string searchPattern = string.Format("#{0}#", key);
bResult = FindAndReplace(wordApp, searchPattern, value);
}
return bResult;
}
private static bool FindAndReplace(Microsoft.Office.Interop.Word.Application wordApp, object findText, object replaceWithText)
{
bool result = false;
object matchCase = true;
object matchWholeWord = true;
object matchWildCards = false;
object matchSoundsLike = false;
object matchAllwordForms = false;
object forward = true;
object format = false;
object matchKashida = false;
object matchDiacritics = false;
object matchAlefHamza = false;
object matchControl = false;
object readOnly = false;
object visible = true;
object replace = 2;
object wrap = 1;
result = wordApp.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord, ref matchWildCards, ref matchSoundsLike, ref matchAllwordForms,
ref forward, ref wrap, ref format, ref replaceWithText, ref replace, ref matchKashida, ref matchDiacritics, ref matchAlefHamza,
ref matchControl);
return result;
}
string map = Server.MapPath(".");
string path = map + "/Files/" + ContractTypesRow.ContractTypeRowID + ".docx";
string FileName = map + "/Files/" + ContractsRow.ContractRowID + ".docx";
string FileName2 = "Files/" + ContractsRow.ContractRowID + ".docx";
bool res = Classes.WordInterop.SaveFile(path, ContractTypesRow.ContractTypeWordTemplateContent,this.Page);
bool res2 = Classes.WordInterop.SaveFile(FileName, ContractTypesRow.ContractTypeWordTemplateContent,this.Page);
if (res && res2)
{
res = Classes.WordInterop.WriteInfoToWordDoc(FileName, data);
if (res)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script type='text/javascript'>"); ;
sb.Append("window.open('" + FileName2 + "','_blank');");
sb.Append("</script>");
if (!ClientScript.IsStartupScriptRegistered("JSScript"))
{
ClientScript.RegisterStartupScript(this.GetType(), "JSScript", sb.ToString());
}
}
}
this is my codes to use it.
thanks..