home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / AUTOWORD.PAK / DRVWORD.CPP next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  2.3 KB  |  98 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectComponents
  3. // Copyright (c) 1996, 1996 by Borland International, All Rights Reserved
  4. //
  5. // $Revision:   2.1  $
  6. //
  7. // Sample illustrating how to Automate MS Word [Office/95 product]
  8. //----------------------------------------------------------------------------
  9. #include <ocf/pch.h>
  10. #include "wb70en32.hxx"
  11.  
  12. // Instance handle of this application
  13. //
  14. HINSTANCE hInstance=0;
  15.  
  16. // Simple routine which binds to Word and creates a simple file with some
  17. // text in it.
  18. //
  19. void
  20. AutomateWord()
  21. {
  22.   WordBasic word;
  23.   try {
  24.  
  25.     // Bind to application
  26.     //
  27.     word.Bind("Word.Basic");
  28.  
  29.     // Talk to application
  30.     //
  31.     try {
  32.  
  33.       // Create a static instance of a noarg to make it easy to have
  34.       // non-specified optional arguments.
  35.       //
  36.       static TNoArg _x;
  37.  
  38.       // Display the application
  39.       //
  40.       word.AppShow(string(""));
  41.  
  42.       // Create a new document
  43.       //
  44.       word.FileNewDefault();
  45.  
  46.       // Put some text in document
  47.       //
  48.       for (int i=0; i<6; i++) {
  49.         word.InsertPara();
  50.         word.Insert("Text inserted by an OCF Automation Controller at ");
  51.         word.InsertDateTime(_x, _x);
  52.  
  53.         // Tweak font for fun!
  54.         //
  55.         word.GrowFont();
  56.       }
  57.  
  58.       // Save new document to current path
  59.       //
  60.       char path[_MAX_PATH];
  61.       ::GetModuleFileName(hInstance, path, sizeof(path));
  62.  
  63.       // NOTE: Cheap way to strip extension from current app's path - Argh!
  64.       //
  65.       char *p = strstr(path, ".EXE");
  66.       if (p) *p = 0;
  67.       word.FileSaveAs(string(path), _x, _x, _x, _x, _x, _x, _x, _x, _x, _x);
  68.     }
  69.  
  70.     // Failure during Automation exchange with application
  71.     //
  72.     catch (xmsg& msg) {
  73.       MessageBox(0, msg.why().c_str(), "Exception Info", MB_OK);
  74.     }
  75.   }
  76.  
  77.   // Following most likely indicates a failure to bind/quit
  78.   //
  79.   catch (xmsg& msg) {
  80.     MessageBox(0, msg.why().c_str(), "Exception Info", MB_OK);
  81.   }
  82. }
  83.  
  84. int PASCAL
  85. WinMain(HINSTANCE hInst, HINSTANCE /*hPrevInstance*/,
  86.         LPSTR /*cmdLine*/, int /*cmdShow*/)
  87. {
  88.   hInstance = hInst;
  89.   try {
  90.     TOleInit initOle;
  91.     AutomateWord();
  92.   }
  93.   catch (xmsg& x) {
  94.     MessageBox(0, x.why().c_str(), "Exception Info", MB_OK|MB_TASKMODAL);
  95.   }
  96.   return 0;
  97. }
  98.