无法在Word中使用C#和Microsoft.Office.Interop.Word查找/替换文本 [英] Cannot find/Replace a text in Word Using C# and Microsoft.Office.Interop.Word

查看:544
本文介绍了无法在Word中使用C#和Microsoft.Office.Interop.Word查找/替换文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我一直试图通过复制另一个预先存在的word文档中的数据来创建word文档。新创建的word文档中的某些字段需要由其他一些文本替换。所以我正在尝试编写一个代码来搜索给定的单词并将其替换为新单词。我遇到的问题是,在运行时,代码尝试访问新创建的word文件并替换它给出此特定错误的那些文本

尝试读取或写入受保护的内存。这通常是表明其他内存已损坏。



以下是我用来替换文本的代码片段





var newdcmnt = new Microsoft.Office.Interop.Word.Document();

var templatepath = Path.Combine(path," OT_1stPage.doc" ); ;

var wordapp = new Microsoft.Office.Interop.Word.Application();

var orgnldoc = wordapp.Documents.Open(templatepath);

orgnldoc.ActiveWindow.Selection.WholeStory();

orgnldoc.ActiveWindow.Selection.Copy();

float TopMargin = orgnldoc.PageSetup.TopMargin;

float BottomMargin = orgnldoc.PageSetup.BottomMargin;

float RightMargin = orgnldoc.PageSetup.RightMargin;

float LeftMargin = orgnldoc.PageSetup.LeftMargin;

newdcmnt.PageSetup.TopMargin = 25.0f;

newdcmnt.PageSetup.BottomMargin = 25.0f;

newdcmnt.PageSetup.RightMargin = 25.0f;

newdcmnt.PageSetup.LeftMargin = 25.0f;

orgnldoc.Close(SaveChanges:false,OriginalFormat:Type.Missing,RouteDocument:Type.Missing);
newdcmnt.ActiveWindow.Selection.PasteAndFormat(Microsoft.Office.Interop.Word.WdRecoveryType.wdPasteDefault);



System.Runtime.InteropServices。 Marshal.ReleaseComObject(wordapp);

System.Runtime.InteropServices.Marshal.ReleaseComObject(orgnldoc);

GC.Collect();

object missingValue = Type.Missing;

foreach(new.cmt.StoryRanges中的Microsoft.Office.Interop.Word.Range tmpRange)

{



tmpRange.Find.Text =xxTenderOpDtxx//我在哪里得到错误

tmpRange.Find.Replacement.Text =SomeText;





tmpRange.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;





object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;





tmpRange.Find.Execute(ref missingValue,ref missingValue,ref missingValue,

ref missingValue,ref missingValue,ref missingValue,ref missingValue,

ref missingValue,ref missingValue,ref missingValue,ref replaceAll,

ref missingValue,ref missingValue,ref missingValue,ref missingValue);

}

newdcmnt.SaveAs(pathString + //+ GlobalVariable.xxTenderNoxx +。doc);

System.Runtime.InteropServices.Marshal.FinalReleaseComObject(newdcmnt);



谢谢

问候

Bonny

解决方案

值得一读下面的文章。应该解决你的问题



尝试读取或写入受保护的内存通常表明其他内存已损坏 [ ^

Hi,
I have been trying to create a word document by copying the data from another pre-existing word document. There are certain fields in the newly created word document which needs to be replaced by some other texts. So i am trying to write a code which would search for the given word and replace it with the new one. The problem I am facing is whenever in the runtime the codes try to access the newly created word file and replace those texts it gives this particular error
"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

Below is the snippet of code that i used to replace the texts


var newdcmnt = new Microsoft.Office.Interop.Word.Document();
var templatepath = Path.Combine(path, "OT_1stPage.doc"); ;
var wordapp = new Microsoft.Office.Interop.Word.Application();
var orgnldoc = wordapp.Documents.Open(templatepath);
orgnldoc.ActiveWindow.Selection.WholeStory();
orgnldoc.ActiveWindow.Selection.Copy();
float TopMargin = orgnldoc.PageSetup.TopMargin;
float BottomMargin = orgnldoc.PageSetup.BottomMargin;
float RightMargin = orgnldoc.PageSetup.RightMargin;
float LeftMargin = orgnldoc.PageSetup.LeftMargin;
newdcmnt.PageSetup.TopMargin = 25.0f;
newdcmnt.PageSetup.BottomMargin = 25.0f;
newdcmnt.PageSetup.RightMargin = 25.0f;
newdcmnt.PageSetup.LeftMargin = 25.0f;
orgnldoc.Close(SaveChanges: false, OriginalFormat: Type.Missing, RouteDocument: Type.Missing);
newdcmnt.ActiveWindow.Selection.PasteAndFormat(Microsoft.Office.Interop.Word.WdRecoveryType.wdPasteDefault);

System.Runtime.InteropServices.Marshal.ReleaseComObject(wordapp);
System.Runtime.InteropServices.Marshal.ReleaseComObject(orgnldoc);
GC.Collect();
object missingValue = Type.Missing;
foreach (Microsoft.Office.Interop.Word.Range tmpRange in newdcmnt.StoryRanges)
{

tmpRange.Find.Text = "xxTenderOpDtxx"//This Where I get the error
tmpRange.Find.Replacement.Text = "SomeText";


tmpRange.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;


object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;


tmpRange.Find.Execute(ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue, ref replaceAll,
ref missingValue, ref missingValue, ref missingValue, ref missingValue);
}
newdcmnt.SaveAs(pathString + "//" + GlobalVariable.xxTenderNoxx + ".doc");
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(newdcmnt);

Thank You
Regards
Bonny

解决方案

Worth Reading below article. Should solve your issue

Attempt to read or write Protected Memory This is often an indicating that other memory is corrupt[^]


这篇关于无法在Word中使用C#和Microsoft.Office.Interop.Word查找/替换文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆