Click here to Skip to main content
14,424,083 members
Rate this:
Please Sign up or sign in to vote.
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()


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.
Posted
Updated 18-Apr-18 20:47pm
v3
Comments
ArunAmalraj 22-Feb-13 6:07am
   
Hello muneebalikiyani,

Check for User Rights.

It will not work if...Your application is running as administrator and WINWORD with normal user rights.
muneebalikiyani 22-Feb-13 6:24am
   
thanks , but what can i do to make it work
Rate this:
Please Sign up or sign in to vote.

Solution 1

Run your service with your credentials - otherwise it won't be able to access Word running with your credentials.
   
Comments
fjdiewornncalwe 22-Feb-13 9:37am
   
My 5.
muneebalikiyani 26-Feb-13 23:19pm
   
I have done that also , i think Marshall.getactiveobject() doesn't works for windows services, please correct me if i am wrong
Member 11615348 15-Apr-16 3:14am
   
Even I am getting the same error:
Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))

I am using VS 2015. Can anybody help on this?
pawan2261872 25-Jan-18 5:03am
   
Your visual syudio might be running in administrator mode.
Both visual studio and the fetching application(word) should be in same mode.
Rate this:
Please Sign up or sign in to vote.

Solution 2

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.
   
Comments
Richard MacCutchan 19-Apr-18 4:06am
   
The question is about using MSWord, not Excel. It was also answered five years ago.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100