Microsoft.Office.Interop错误0x800401E3
我试图通过我的服务访问Microsoft word实例但我收到此错误
操作不可用(HRESULT异常:0x800401E3(MK_E_UNAVAILABLE)) ,
我已打开word文档(我也可以在任务管理器中看到WINWORD.EXE),
我正在使用VS 2010和MS Office 2003.这里是我的代码
I am trying to access Microsoft word instance through my service but I am getting this error
Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE)) ,
I have opened word document ( i can also see WINWORD.EXE in Task Manager) ,
I am using VS 2010 and MS Office 2003. Here is my code
Dim fs As New FileStream("D:\log.txt", FileMode.OpenOrCreate, FileAccess.Write)
Dim sw As New StreamWriter(fs)
sw.BaseStream.Seek(0, SeekOrigin.End)
Dim wordapp As Word.Application
wordapp = Marshal.GetActiveObject("Word.Application")
For Each doc As Word.Document In wordapp.Documents
sw.WriteLine(doc.FullName.ToString() + "\n" +
doc.ActiveWindow.WindowState.ToString())
Next
sw.Flush()
sw.Close()
如果我在Windows窗体应用程序中使用此代码它完美,但在Windows服务中不起作用。为什么这样做? Windows服务不支持Microsoft.Office.Interop?如果确实有效,请指导。
if i use this code in windows form application it works perfect, but doesn’t works in windows service.Why is that for ? Windows Service Doesn’t support Microsoft.Office.Interop ? If it does work please guide.
使用您的凭据运行您的服务 - 否则它将无法访问使用您的凭据运行的Word。
Run your service with your credentials - otherwise it won't be able to access Word running with your credentials.
Add reference only of "Microsoft.Office.Interop.Excel.dll"
try
{
//This requires excel app (excel.exe*32) to be running means any excel sheet should be open. If excel not running then it will throw error.
excelApp = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
excelApp.Visible = false;
}
catch
{
//create new excel instance
excelApp = new Excel.Application();
excelApp.Visible = false;
}
This worked for me.
Advantage: No need to copy Microsoft.Office.Interop.Excel.dll to your installed foler. Since MS excel is installed it will take from GAC.
I used Office.dll too but not sure if its really required.