Automation of MS Word 2000

Posted on 2003-03-24
Medium Priority
Last Modified: 2011-10-03
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.
Question by:scott769
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

Accepted Solution

corduroy9 earned 280 total points
ID: 8196855

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;


Expert Comment

ID: 9474498
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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

770 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