• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 386
  • Last Modified:

Automation of MS Word 2000

I need to automate MS Word 2000 so that I can take a list of files, open them, read an email address contained in the first line of the document, and then send that document to the specified email address.  This is what I have so far, which isn't very much at all...and I got this from MSDN.  Thank you for any help. - Scott

    // Convenient values declared as ColeVariants.
    COleVariant covTrue((short)TRUE),
                covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

    _Application app;  // app is the Word _Application object

    // Start Word and get Application object...
       AfxMessageBox("Couldn't start Word.");
       //Make Word Visible and display a message

        Documents docs(app.GetDocuments());
       _Document testDoc;

      CString fileName = "C:\\Documents and Settings\\Administrator\\Desktop\\parsing.doc";
      testDoc.AttachDispatch(docs.Open(     COleVariant(fileName,VT_BSTR),                                             covFalse,    // Confirm Conversion.
                         covFalse,    // ReadOnly.
                         covFalse,    // AddToRecentFiles.
                         covOptional, // PasswordDocument.
                         covOptional, // PasswordTemplate.
                         covFalse,    // Revert.
                         covOptional, // WritePasswordDocument.
                         covOptional, // WritePasswordTemplate.
                         covOptional, // Format. // Last argument for Word 97
                         covOptional, // Encoding // New for Word 2000/2002
                         covTrue)     // Visible

          /*          What Goes Here??         */
       app.Quit(covFalse,  // SaveChanges.
                   covTrue,   // OriginalFormat.
                   covFalse   // RouteDocument.
1 Solution

I'm pasting code that I use to save a Word 2000 document as a TXT file.  Then you can use regular fopen, fgets, fclose functions for getting the file contents.  

#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");
     // return error;
   // Open the document
   _variant_t document_filename(sFileName.GetBuffer(0));
   Word::_DocumentPtr document = word_app->Documents->Open(&document_filename);
   if(document == NULL)
     // 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;

This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Post your closing recommendations!  No comment means you don't care.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now