在asp.net中的word模板上的问题 [英] problem on word template in asp.net
问题描述
我每个人都在做一个项目,我需要创建带有标签的单词模板,
i创建的方法以及它工作得很好但是当我在我的laptap上获得项目时它出现在
此错误
尝试读取或写入受保护的内存。 '〜/ file / xxx.docx'这通常表示其他内存已损坏。在asp.net中处理文件
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;
// setup Word.Application class.
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
// setup Word.Document class we'll use
Microsoft.Office.Interop.Word.Document wordDoc = null;
object readOnly = false;
object isVisible = false;
try
{
// set word to be not visible
wordApp.Visible = false;
// open the word document
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);
// activate the document
wordDoc.Activate();
foreach (string key in data.Keys)
{
WriteData(wordApp, key, data[key]);
}
// save the document to correct destination
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);
// close the document
wordDoc.Close(ref missing, ref missing, ref missing);
result = true;
}
catch (Exception ex)
{
// nothing!
}
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;
}
// how i use this functionality
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..
推荐答案
试图阅读或写保护的内存。 '〜/ file / xxx.docx'
Attempted to read or write protected memory. '~/file/xxx.docx'
我想它在你的笔记本电脑中受到保护。这可能是由于Microsoft .NET对该文件夹或目录或文件的访问受限。
因此,右键单击文件,然后检查属性并查看安全设置。您已授予适当的权限,
否则,尝试在管理员模式下运行 Visual Studio 并查看是否是否工作。
I guess it is protected in your laptop. That might be due to limited access for Microsoft .NET to that folder or directory or file.
So, Right Click on the file, then check the Properties and see the Security Settings. You have give appropriate permissions,
Otherwise, try to run Visual Studio in Administrator mode and see if it works or not.
嗨Tadit Dash。
关于我的问题我决定在IIS上测试问题,看看我最终会遇到这个错误
由于以下错误,检索CLSID为{000209FF-0000-0000-C000-000000000046}的组件的COM类工厂失败: 80070005访问被拒绝。 (来自HRESULT的异常:0x80070005(E_ACCESSDENIED))。
i挖掘此错误,我发现它必须是Decoms在组件服务上的权限
当我打开我的组件服务时,decom是微软的一句话我没有发现Decom
i删除了所有办公室和安装时间之后Decom在组件服务中
i测试应用程序agein问题消失。
但不是票价,因为问题与此事无关或错误不明确。
感谢您的帮助Tadit Dash。
hi Tadit Dash.
about My problem i decide to test problem on IIS and see whats coming i endup with this error
Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).
i dig on this error and i found it has to be permissions of Decoms on Component Services
the decom was Microsoft word when i opened my Component Services i did not found that Decom
i removed all office and instal agein after that Decom was in Component Services
i test application agein problem dispeared.
but it not fare because problem did not related to this matter or error was not clear.
thanks for help Tadit Dash.
这篇关于在asp.net中的word模板上的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!