MS Word Automation and Windows Services

I am trying to run my application as a Windows service.  I have written many Windows Services and never had much problem with them.  Now this one...

This program uses OLE objects and automation to save a Microsoft Word .doc file in a .txt format, which allows me to parse through the file data more easily.

Everytime I call the CreateInstance function, I get a general access error (80070005 in winerror.h).  

Does anyone know how to get around this access error and still run as a Service?  It appears the Service wrapper is preventing me from using the automation code.

here is some sample code for MS Word automation:  (i would provide service code as well, but it's too much for here)

#pragma warning (disable:4146)
#import "mso.dll"
#pragma warning (default:4146)
#import "vbe6ext.olb"
#import "msword.olb" rename("ExitWindows", "WordExitWindows")

void main ( void )

     CString sFileName = "c:\\test.doc";
               // Create and initialize the word document
               Word::_ApplicationPtr word_app;

               Word::FindPtr find_app;
               HRESULT hr = word_app.CreateInstance("Word.Application");
                     cout << "Could not create instance of Word pointer " << hr << endl;// return error;

               // Open the document
               _variant_t document_filename(sFileName.GetBuffer(0));
               Word::_DocumentPtr document = word_app->Documents->Open(&document_filename);
               if(document == NULL)
                     cout << "Could not open file " << endl;// return error;

               //  PROPID
               VARIANT vtFile;
               VARIANT vtFileFormat;
               vtFile.vt = VT_BSTR;
               _bstr_t strTrip( sFileName.GetBuffer(0) );
               vtFile.bstrVal = strTrip;

//wdFormatDocument = 0,
//wdFormatTemplate = 1,
//wdFormatText = 2,
//wdFormatTextLineBreaks = 3,
//wdFormatDOSText = 4,
//wdFormatDOSTextLineBreaks = 5,
//wdFormatRTF = 6,
//wdFormatUnicodeText = 7,
//wdFormatEncodedText = 7,
//wdFormatHTML = 8,
//wdFormatWebArchive = 9,
//wdFormatFilteredHTML = 10

               vtFileFormat.vt      = VT_I2;
               vtFileFormat.iVal = 2;

               //_variant_t vtSaveFormat(Word::WdSaveFormat->wdFormatText);
               _variant_t vtSaveFormat("2");
               document->SaveAs( &vtFile, &vtFileFormat );
               // Close the document and exit the app
          catch(_com_error &e)
               _bstr_t bstrDescription(e.Description());
               CString sErrorCode, sError;
               sErrorCode.Format(  "%08lx", e.Error());
               sError = (LPCSTR) bstrDescription;

               cout << sError;



Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Once upon a time I had an issue like this, where the Word document would be created just fine if a logged in user created it, but wouldn't work if a service or ISAPI module tried to use it.  It was related to the fact that the user that runs the service (which is the system user) doesn't have an Office profile.  I went into dcomcnfg, went into "Default Security" changed the "Default Launch Permissions" and added the IUSR and IWAM users to the list of users that could launch the application.  I hope this helps.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
corduroy9Author Commented:

Hey!  Thanks a lot mnashadka.  It worked well.  I also had to change the Default Access Permissions in dcomcnfg, adding the user that I have as the Log On user for the Service.   Thanks again!

Glad I could help.  I just can't figure out why Excel worked fine without modifying these permissions, but Word had problems.  Either way, good luck.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.