PageRenderTime 32ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/Visual Studio 2008/CppAutomatePowerPoint/ReadMe.txt

#
Plain Text | 208 lines | 146 code | 62 blank | 0 comment | 0 complexity | 5603d05bdadc9a2586db64f0dfcb6bbe MD5 | raw file
  1. ========================================================================
  2. CONSOLE APPLICATION : CppAutomatePowerPoint Project Overview
  3. ========================================================================
  4. /////////////////////////////////////////////////////////////////////////////
  5. Summary:
  6. The CppAutomatePowerPoint example demonstrates how to write VC++ code to
  7. create a Microsoft PowerPoint instance, create a new presentation, insert a
  8. new slide, add some texts to the slide, save the presentation, quit
  9. PowerPoint and then clean up unmanaged COM resources.
  10. There are three basic ways you can write VC++ automation codes:
  11. 1. Automating PowerPoint using the #import directive and smart pointers
  12. The code in Solution1.h/cpp demonstrates the use of #import to automate
  13. PowerPoint. #import (http://msdn.microsoft.com/en-us/library/8etzzkb6.aspx),
  14. a new directive that became available with Visual C++ 5.0, creates VC++ "smart
  15. pointers" from a specified type library. It is very powerful, but often not
  16. recommended because of reference-counting problems that typically occur when
  17. used with the Microsoft Office applications. Unlike the direct API approach
  18. in Solution2.h/cpp, smart pointers enable us to benefit from the type info to
  19. early/late bind the object. #import takes care of adding the messy guids to
  20. the project and the COM APIs are encapsulated in custom classes that the
  21. #import directive generates.
  22. 2. Automating PowerPoint using C++ and the COM APIs
  23. The code in Solution2.h/cpp demontrates the use of C/C++ and the COM APIs to
  24. automate PowerPoint. The raw automation is much more difficult, but it is
  25. sometimes necessary to avoid the overhead with MFC, or problems with #import.
  26. Basically, you work with such APIs as CoCreateInstance(), and COM interfaces
  27. such as IDispatch and IUnknown.
  28. 3. Automating PowerPoint with MFC
  29. With MFC, Visual C++ Class Wizard can generate "wrapper classes" from the
  30. type libraries. These classes simplify the use of the COM servers. Automating
  31. PowerPoint with MFC is not covered in this sample.
  32. /////////////////////////////////////////////////////////////////////////////
  33. Prerequisite:
  34. You must run this code sample on a computer that has Microsoft PowerPoint
  35. 2007 installed.
  36. /////////////////////////////////////////////////////////////////////////////
  37. Demo:
  38. The following steps walk through a demonstration of the PowerPoint automation
  39. sample that starts a Microsoft PowerPoint instance, adds a new presentation,
  40. inserts a slide, adds some texts to the slide, saves the presentation, quits
  41. PowerPoint and then cleans up unmanaged COM resources.
  42. Step1. After you successfully build the sample project in Visual Studio 2008,
  43. you will get the application: CppAutomatePowerPoint.exe.
  44. Step2. Open Windows Task Manager (Ctrl+Shift+Esc) to confirm that no
  45. powerpnt.exe is running.
  46. Step3. Run the application. It should print the following content in the
  47. console window if no error is thrown.
  48. PowerPoint.Application is started
  49. A new presentation is created
  50. Insert a slide
  51. Add some texts
  52. Save and close the presentation
  53. Quit the PowerPoint application
  54. PowerPoint.Application is started
  55. A new presentation is created
  56. The presentation currently has 0 slides
  57. Insert a slide
  58. Add some texts
  59. Save and close the presentation
  60. Quit the PowerPoint application
  61. Then, you will see two new presentation files in the directory of the
  62. application: Sample1.pptx and Sample2.pptx. Both presentations contain only
  63. one slide with the following title.
  64. All-In-One Code Framework
  65. Step4. In Windows Task Manager, confirm that the powerpnt.exe process does
  66. not exist, i.e. the Microsoft PowerPoint intance was closed and cleaned up
  67. properly.
  68. /////////////////////////////////////////////////////////////////////////////
  69. Project Relation:
  70. CppAutomatePowerPoint - CSAutomatePowerPoint - VBAutomatePowerPoint
  71. These examples automate Microsoft PowerPoint to do the same thing in
  72. different programming languages.
  73. /////////////////////////////////////////////////////////////////////////////
  74. Creation:
  75. A. Automating Word using the #import directive and smart pointers
  76. (Solution1.h/cpp)
  77. Step1. Import the type library of the target COM server using the #import
  78. directive.
  79. #import "libid:2DF8D04C-5BFA-101B-BDE5-00AA0044DE52" \
  80. rename("RGB", "MSORGB")
  81. rename("DocumentProperties", "MSODocumentProperties")
  82. // [-or-]
  83. //#import "C:\\Program Files\\Common Files\\Microsoft Shared\\OFFICE12\\MSO.DLL" \
  84. // rename("RGB", "MSORGB")
  85. // rename("DocumentProperties", "MSODocumentProperties")
  86. using namespace Office;
  87. #import "libid:0002E157-0000-0000-C000-000000000046"
  88. // [-or-]
  89. //#import "C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA6\\VBE6EXT.OLB"
  90. using namespace VBIDE;
  91. #import "libid:91493440-5A91-11CF-8700-00AA0060263B"
  92. // [-or-]
  93. //#import "C:\\Program Files\\Microsoft Office\\Office12\\MSPPT.OLB"
  94. Step2. Build the project. If the build is successful, the compiler generates
  95. the .tlh and .tli files that encapsulate the COM server based on the type
  96. library specified in the #import directive. It serves as a class wrapper we
  97. can now use to create the COM class and access its properties, methods, etc.
  98. Step3. Initializes the COM library on the current thread and identifies the
  99. concurrency model as single-thread apartment (STA) by calling CoInitializeEx,
  100. or CoInitialize.
  101. Step4. Create the PowerPoint.Application COM object using the smart pointer.
  102. The class name is the original interface name (i.e. PowerPoint::_Application)
  103. with a "Ptr" suffix. We can use either the constructor of the smart pointer
  104. class or its CreateInstance method to create the COM object.
  105. Step5. Automate the PowerPoint COM object through the smart pointers. In this
  106. example, you can find the basic operations in PowerPoint automation like
  107. Create a new Presentation. (i.e. Application.Presentations.Add)
  108. Insert a slide.
  109. Add some texts,
  110. Save the presentation as a pptx file and close it.
  111. Step6. Quit the PowerPoint application. (i.e. Application.Quit())
  112. Step7. It is said that the smart pointers are released automatically, so we
  113. do not need to manually release the COM object.
  114. Step8. It is necessary to catch the COM errors if the type library was
  115. imported without raw_interfaces_only and when the raw interfaces
  116. (e.g. raw_Quit) are not used. For example:
  117. #import "XXXX.tlb"
  118. try
  119. {
  120. spPpApp->Quit();
  121. }
  122. catch (_com_error &err)
  123. {
  124. }
  125. Step9. Uninitialize COM for this thread by calling CoUninitialize.
  126. -----------------------------------------------------------------------------
  127. B. Automating PowerPoint using C++ and the COM APIs (Solution2.h/cpp)
  128. Step1. Add the automation helper function, AutoWrap.
  129. Step2. Initialize COM by calling CoInitializeEx, or CoInitialize.
  130. Step3. Get CLSID of the PowerPoint COM server using the API CLSIDFromProgID.
  131. Step4. Start the PowerPoint COM server and get the IDispatch interface using
  132. the API CoCreateInstance.
  133. Step5. Automate the PowerPoint COM object with the help of AutoWrap. In this
  134. example, you can find the basic operations in PowerPoint automation like
  135. Create a new Presentation. (i.e. Application.Presentations.Add)
  136. Insert a slide.
  137. Add some texts,
  138. Save the presentation as a pptx file and close it.
  139. Step6. Quit the PowerPoint application. (i.e. Application.Quit())
  140. Step7. Release the COM objects.
  141. Step8. Uninitialize COM for this thread by calling CoUninitialize.
  142. /////////////////////////////////////////////////////////////////////////////
  143. References:
  144. MSDN: PowerPoint 2007 Developer Reference
  145. http://msdn.microsoft.com/en-us/library/bb265982.aspx
  146. /////////////////////////////////////////////////////////////////////////////