正确释放WORD对象(COM组件) COMException: 被调用的对象已与其客户端断开连接
本来form method=post本页面
修改为其他页面 action=save.aspx后没问题
其他问题可参考以下:
引自:http://topic.csdn.net/u/20090108/17/f240cd4d-72cf-44bc-851e-cc587dd7e468.html
源问题:
详细内容:
System.Runtinm.InteropServices.COMException
被调用的对象已与其客户端断开连接。 (异常来自 HRESULT:0x80010108 (RPC_E_DISCONNECTED))
ErrorCode:-2147417848
Souce:Interop.Word
StackTrace:" 在 Word.ApplicationClass.Quit(Object& SaveChanges, Object& OriginalFormat, Object& RouteDocument)\r\n 在 WordOPTools.OpWord(Object& fileName,
Boolean issafe, String newpath)"
当处理的word内容比较大的时候报这个异常.
还请各位多多帮忙解决一下这个问题.
解决方法:
object nothing=System.Reflection.Missing.Value;
object optional=System.Reflection.Missing.Value;
object visible=true;
object saveChanges = true;
object NOTsaveChanges = false;
object docreadonly=true;
object originalFormat = System.Reflection.Missing.Value;
object routeDocument =System.Reflection.Missing.Value;
Word.ApplicationClass app=new Word.ApplicationClass();
object Fi=page.Server.MapPath(strC+"Template_temp/"+FileName);
Word.Document Doc=app.Documents.Open(ref Fi,ref optional,ref docreadonly,ref optional,ref optional,ref optional,ref optional,ref optional,ref optional,ref
optional,ref optional, ref visible);
Doc.SaveAs(ref strFileName,ref optional, ref optional, ref optional,ref optional, ref optional, ref optional,ref optional, ref optional, ref optional, ref
optional);
Doc.Close(ref NOTsaveChanges, ref originalFormat, ref routeDocument);
app.Quit(ref NOTsaveChanges, ref originalFormat, ref routeDocument);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
System.Runtime.InteropServices.Marshal.ReleaseComObject(Doc);
app=null;
Doc=null;
GC.Collect();
GC.Collect();
--------------------------------------------------------------
补充
------------摘自:http://blogs.geekdojo.net/richardhsu/archive/2003/11/14/281.aspx
Working with COM Exe in C#
Credit goes to Peter A. Bromberg for this one. In his article, he talks about creating dynamic excel workbooks in C# within an ASP.NET page.
What I learnt from the article was how we can properly release a COM object. There are two methods that we are gets the deallocation done. They are :-
1) GC.Collect(); // this one forces garbage collection
2) System.Runtime.InteropServices.Marshal.ReleaseComObject (object); // this one releases the passed in COM object wrapper instance
The pattern the author (Peter A. Bromberg) uses to create and release COM Wrapped Excel objects is :-
1) call GC.Collect() to force collection of existing COM Objects waiting to be released.
2) instantiate the COM Wrapped Excel objects (Workbook, Worksheet etc.)
3) do the thing...
4) call the Close() or Quit() methods on the objects when done.
5) call System.Runtime.InteropServices.Marshal.ReleaseComObject(object) once for each COM object created.
6) set each object variable to null.
7) call GC.Collect() again
8) be relieved and reminisce the cool VB6 way of doing the above (Set obj = Nothing)
Here is a slightly altered & annotated code fragment (in C#) that shows how it is done :-
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
// Step 1
GC.Collect();// clean up any other excel guys hangin' around...
// Step 2
oXL = new Excel.Application();
oWB = (Excel._Workbook)(oXL.Workbooks.Add( Missing.Value ));
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
// Step 3
// this part will actually be filling in the values into the sheet
fillValues(oSheet);
....
// Step 4
// Need all following code to clean up and extingush all references!!!
oWB.Close(null,null,null);
oXL.Workbooks.Close();
oXL.Quit();
// Step 5
System.Runtime.InteropServices.Marshal.ReleaseComObject (oXL);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oWB);
// Step 6
oSheet=null;
oWB=null;
oXL = null;
// Step 7
GC.Collect(); // force final cleanup!
Although I am yet to fully understand what goes on behind the scenes, I have used the above mention pattern, and it works. Excel exposes its functionality through a COM Exe. Maybe, we don't need to do all this for a COM Dll, but that is for later.
Published Friday, November 14, 2003 5:08 PM by richardhsu
Filed Under: Office
- VBA, C#
正确释放WORD对象(COM组件) COMException: 被调用的对象已与其客户端断开连接的更多相关文章
- Qt Quick 组件和动态创建的对象具体的解释
在<Qt Quick 事件处理之信号与槽>一文中介绍自己定义信号时,举了一个简单的样例.定义了一个颜色选择组件,当用户在组建内点击鼠标时,该组件会发出一个携带颜色值的信号,当时我使用 Co ...
- 为什么new的普通数组用delete 和 delete[]都能正确释放
由同事推荐的一篇博客: 为何new出的对象数组必须要用delete[]删除,而普通数组delete和delete[]都一样-------_CrtMemBlockHeader 文章解释了delete 内 ...
- ArcGIS Engine中正确释放打开资源
转自原文 ArcGIS Engine中正确释放打开资源 AE中对MDB,SDE等数据库操作时,打开后却往往不能及时释放资源,导致别人操作提示对象被锁定. 很多帖子说了很多原理,看的也烦且不实用,比如一 ...
- .NET平台开源项目速览(14)最快的对象映射组件Tiny Mapper
好久没有写文章,工作甚忙,但每日还是关注.NET领域的开源项目.五一休息,放松了一下之后,今天就给大家介绍一个轻量级的对象映射工具Tiny Mapper:号称是.NET平台最快的对象映射组件.那就一起 ...
- .NET平台开源项目速览(2)Compare .NET Objects对象比较组件
.NET平台开源项目速览今天介绍一款小巧强大的对象比较组件.可以更详细的获取2个对象的差别,并记录具体差别,比较过程和要求可以灵活配置. .NET开源目录:[目录]本博客其他.NET开源项目文章目录 ...
- .NET平台开源项目速览-最快的对象映射组件Tiny Mapper之项目实践
心情小札:近期换了工作,苦逼于22:00后下班,房间一篇狼藉~ 小翠鄙视到:"你就适合生活在垃圾堆中!!!" 晚上浏览博客园 看到一篇非常实用的博客:.NET平台开源项目速览(14 ...
- Unity3d修炼之路:载入一个预制体,然后为该对象加入组件,然后查找对象,得到组件。
#pragma strict function Awake(){ //载入一个预制体 资源必须在 Resources目录下 Resources.LoadLoad(); //载入后 必须演示样例化 Ga ...
- Tiny Mapper是一个.net平台开源的对象映射组件
NET平台开源项目速览(14)最快的对象映射组件Tiny Mapper 阅读目录 1.Tiny Mapper基本介绍 2.Tiny Mapper 基本使用 3.Tiny Mapper 指定配置使用 ...
- Compare .NET Objects对象比较组件
Compare .NET Objects对象比较组件 阅读目录 1.Compare .NET Objects介绍 2. Compare .NET Objects注意事项 3.一个简单的使用案例 4.三 ...
随机推荐
- C/C++ 下的void main()
很多人甚至市面上的一些书籍,都使用了void main( ),其实这是错误的.C/C++ 中从来没有定义过void main( ).C++之 父 Bjarne Stroustrup在他的主页上的 FA ...
- php 日期和时间
php date() 函数把时间戳格式化为更易读取的日期和时间 语法: date(formet,timestamp); 参数 描述 format 必需.规定时间戳的格式. timestamp 可选.规 ...
- c# 上传图片流,php端(laravel框架)接收处理方法
c# httppost方法 public struct PostFile { public string name; public string filename; public Stream bit ...
- look-into-oracle-redo
https://fritshoogland.wordpress.com/2018/02/05/a-look-into-oracle-redo-part-2-the-discovery-of-the-k ...
- 写一个针对IQueryable<T>的扩展方法支持动态排序
所谓的动态排序是指支持任意字段.任意升序降序的排序.我们希望在客户端按如下格式写: localhost:8000/api/items?sort=titlelocalhost:8000/api/item ...
- 1.2 Stream API
引例: List<String> strList = Arrays.asList("zhaojigang","nana","tianya& ...
- C++11 多线程编程 使用lambda创建std::thread (生产/消费者模式)
要写个tcp server / client的博客,想着先写个c++11多线程程序.方便后面写博客使用. 目前c++11中写多线程已经很方便了,不用再像之前的pthread_create,c++11中 ...
- UVA 10303 - How Many Trees?(数论 卡特兰数 高精度)
Problem D How Many Trees? Input: standard input Output: standard output Memory Limit: 32 MB A binary ...
- vscode下调试caffe源码
caffe目录: ├── build -> .build_release // make生成目录,生成各种可执行bin文件,直接调用入口: ├── cmake ├── CMakeLists.tx ...
- 马斯克:有62%的程序员认为人工智能会被武器化 #精选AR人工智能算法
当地时间 9 月 13 日,马斯克在自己的个人推特账号上转推了一篇名为<Hackers Have Already Started to Weaponize Artificial Intellig ...