我正在尝试通过我的服务(Windows 服务)访问 Microsoft Word 实例,但出现此错误:
操作不可用(HRESULT 异常:0x800401E3 (MK_E_UNAVAILABLE))
我打开了一个word文档(我也可以在任务管理器中看到WINWORD.EXE)。我正在使用 VS 2010 和 MS Office 2003。这是我的代码:
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("Microsoft.Office.Interop.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 Service中不起作用。这是为什么?Windows 服务不支持Microsoft.Office.Interop
?如果它确实有效,请帮忙?