MS Word Automation and Windows Services

Posted on 2003-02-25
Medium Priority
Last Modified: 2007-12-19

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;



Question by:corduroy9
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

mnashadka earned 280 total points
ID: 8021371
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.

Author Comment

ID: 8024782

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!


Expert Comment

ID: 8024830
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.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question