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