Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

MS Word Automation and Windows Services

Posted on 2003-02-25
3
Medium Priority
?
786 Views
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";
     CoInitialize(NULL);
     {
          try
          {
               // Create and initialize the word document
               Word::_ApplicationPtr word_app;

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

               // 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;
                     return;
               }
               

               //  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
               document->Close();
               word_app->Application->Quit();
          }
          catch(_com_error &e)
          {
               _bstr_t bstrDescription(e.Description());
               
               CString sErrorCode, sError;
               sErrorCode.Format(  "%08lx", e.Error());
               sError = (LPCSTR) bstrDescription;

               cout << sError;

               return;
          }
     }

     CoUninitialize();
     return;
}



0
Comment
Question by:corduroy9
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
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.
0
 
LVL 2

Author Comment

by:corduroy9
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!

0
 
LVL 8

Expert Comment

by:mnashadka
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.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

578 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