日期:2014-05-17  浏览次数:20497 次

求助~~~~~~~如何在ASP.NET中得到当前进程中已经运行的word程序的引用, Marshal.GetActiveObject()在winform中可以但在ASP.NET中不行

下面这种写法在C# WINDOWS窗体程序中可以通过,即WINFORM中可以,但在ASP.NET就会报异常

-----------------------------

  Microsoft.Office.Interop.Word.ApplicationClass wordApp = null;
  try
  {
  wordApp = Marshal.GetActiveObject( "Word.Application" ) as Microsoft.Office.Interop.Word.ApplicationClass;
  }


-----------------------------
我在国外一个论坛里搜索一个帖子和我的问题差不多,帖子的地址如下:
http://www.developmentnow.com/g/16_2005_1_0_0_47728/Marshal-GetActiveObject-throws-an-exception-in-a-C-webservice-but.htm


里面有位老大好像说出了其中的原因(如下),但该帖好像最终也没有给出解决的方法,所以我就郁闷了。。。。。:(
-------------------------------------------------------
Hello,

Based on my research, GetActiveObject obtains a running instance of an 
object from the running object table (ROT). Each WinStation has it's own 
ROT. The interactive desktop is one WinStation and has it's own ROT and 
that is where the desktop application will have registered the running 
object. The ASP.NET process is running under a seperate account and exists 
in a non-interactive WinStation with its own ROT. That is why it fails.

I think the only way for this to work would be to launch the third party 
application process from ASP.NET so it exists in the same WinStation. Of 
course the application will then not be visible and the user cannot 
interact with it.
-------------------------------------------------------

请各位CSDN的大侠相助,小弟不胜感激!

另外,我们的项目需求必须“是在ASP.NET的网页中去得到当前运行的word的实例,而不是自己去new一个,new一个我们可以使用Activator.CreateInstance()成功实现了,但是Get一个当前已经运行的对象就不行”。


------解决方案--------------------
没看明白英文的意思。。。
反射的实例没问题,可能方法没有,
你单步调试试试。。
------解决方案--------------------
Asp.NET中也可以。
在ASP.NET中使用Microsoft Word文档
------解决方案--------------------
探讨
感谢 wonsoft 的回复,但你这个帖子里是去打开了一个新的,即new了一个,但我们需要的是得到一个当前已经运行了的word的引用,而不是新打开一个。
我在提问的最后也这样说了:
-----------------------------------------------

“是在ASP.NET的网页中去得到当前运行的word的实例,而不是自己去new一个,new一个我们可以使用Activator.CreateInstance()成功实现了,但是Get一个当前已经运行的对象就不行”。


Anywa…

------解决方案--------------------
看看下面的写法, 如果有活动对象getObject,否则new:

Microsoft.Office.Interop.Excel.Application ap;
try
{
ap =(Microsoft.Office.Interop.Excel.Application)
Microsoft.VisualBasic.Interaction.GetObject(null, "Excel.Application");
}
catch
{
ap = new Microsoft.Office.Interop.Excel.Application();
}

------解决方案--------------------
1.确定服务器上有word
2.确定当前已经有了活动实例
3.确定asp.net启动账户与word启动账户相同