当前位置:编程学习 > C#/ASP.NET >>

尝试读取或写入受保护的内存。这通常指示其他内存已损坏(c#操作word)--非常急-谢谢大虾们了

public static void copyWordDoc(string sorceDocPaths, string savePaths)
        {
            object objDocType = WdDocumentType.wdTypeDocument;
            object type = WdBreakType.wdSectionBreakContinuous;

            _Application wordApp;
            Document newWordDoc;

            object readOnly = false;
            object isVisible = false;
            object sorceDocPath = sorceDocPaths;
            object savePath = savePaths;
            wordApp = new ApplicationClass();

            Object Nothing = System.Reflection.Missing.Value;

            //wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);     
            newWordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

            _Document openWord;
            openWord = wordApp.Documents.Open(ref sorceDocPath, ref Nothing, ref readOnly, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref isVisible, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
            openWord.Select();
            openWord.Sections[1].Range.Copy();

            object start = 0;
            Range newRang = newWordDoc.Range(ref start, ref start);

     
            //newWordDoc.Sections[1].Range.InsertBreak(ref type); 
            newWordDoc.Sections[1].Range.PasteAndFormat(WdRecoveryType.wdPasteDefault);
            openWord.Close(ref Nothing, ref Nothing, ref Nothing);






            object format = WdSaveFormat.wdFormatDocument;
            string strOldText = "{WORD}";
            string strNewText = "移动";
            List<string> IListOldStr = new List<string>();
            IListOldStr.Add("联想");
            IListOldStr.Add("{WORD2}");
           // Microsoft.Office.Interop.Word.Application wordApp = new ApplicationClass();
            //Microsoft.Office.Interop.Word.Document oDoc = wordApp.Documents.Open(ref obj, ref Nothing, ref readOnly, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref isVisible, ref Nothing, ref Nothing, ref Nothing, ref Nothing); 
            _Document oDoc = newWordDoc;

            object FindText, ReplaceWith, Replace;
            object MissingValue = Type.Missing;

            foreach (string str in IListOldStr)
            {

                oDoc.Content.Find.Text = str;//在这里报错---尝试读取或写入受保护的内存。这通常指示其他内存已损坏
                FindText = str;
                ReplaceWith = strNewText;

                 Replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;

                 oDoc.Content.Find.ClearFormatting();

               }

            oDoc.SaveAs(ref savePath, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                       oDoc.Close(ref Nothing, ref Nothing, ref Nothing);
                        wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
        }
--------------------编程问答-------------------- 恩,我是低手,等楼下的高手解决吧~ --------------------编程问答-------------------- 设置断点调试下
看看IListOldStr是否获取到了内容 --------------------编程问答-------------------- IListOldStr 取到内容了,就是把内容赋给它的时候出错,(oDoc.Content.Find.Text = str) --------------------编程问答-------------------- 看看这个方法行不行。


'Find and replace some text  
        'Replace 'VB' with 'Visual Basic'  
        objDoc.Content.Find.Execute(FindText:="VB", ReplaceWith:="Visual Basic Express", Replace:=Word.WdReplace.wdReplaceAll)  
        While objDoc.Content.Find.Execute(FindText:="  ", Wrap:=Word.WdFindWrap.wdFindContinue)  
            objDoc.Content.Find.Execute(FindText:="  ", ReplaceWith:=" ", Replace:=Word.WdReplace.wdReplaceAll, Wrap:=Word.WdFindWrap.wdFindContinue)  
        End While 


代码来自:
http://social.msdn.microsoft.com/Forums/en-US/vbide/thread/1558f560-8ae6-413a-bbc4-aa8d5d912aac --------------------编程问答-------------------- 请问楼主有解决问题吗?相同的问题。。。 --------------------编程问答--------------------
引用 3 楼 zhaodianshigan 的回复:
IListOldStr 取到内容了,就是把内容赋给它的时候出错,(oDoc.Content.Find.Text = str)

请问解决了没
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,