PageRenderTime 39ms CodeModel.GetById 8ms app.highlight 22ms RepoModel.GetById 2ms app.codeStats 0ms

/Visual Studio 2008/CppAutomatePowerPoint/Solution1.cpp

#
C++ | 223 lines | 66 code | 48 blank | 109 comment | 2 complexity | 2b0c92f335a52ee0ac6f0dc1a67f52fc MD5 | raw file
  1/****************************** Module Header ******************************\
  2* Module Name:  Solution1.cpp
  3* Project:      CppAutomatePowerPoint
  4* Copyright (c) Microsoft Corporation.
  5* 
  6* The code in Solution1.h/cpp demonstrates the use of #import to automate 
  7* PowerPoint. #import (http://msdn.microsoft.com/en-us/library/8etzzkb6.aspx), 
  8* a new directive that became available with Visual C++ 5.0, creates VC++ 
  9* "smart pointers" from a specified type library. It is very powerful, but 
 10* often not recommended because of reference-counting problems that typically 
 11* occur when used with the Microsoft Office applications. Unlike the direct 
 12* API approach in Solution2.h/cpp, smart pointers enable us to benefit from 
 13* the type info to early/late bind the object. #import takes care of adding 
 14* the messy guids to the project and the COM APIs are encapsulated in custom 
 15* classes that the #import directive generates.
 16* 
 17* This source is subject to the Microsoft Public License.
 18* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
 19* All other rights reserved.
 20* 
 21* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 22* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 23* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
 24\***************************************************************************/
 25
 26#pragma region Includes
 27#include <stdio.h>
 28#include <windows.h>
 29#include "Solution1.h"
 30#pragma endregion
 31
 32
 33#pragma region Import the type libraries
 34
 35#import "libid:2DF8D04C-5BFA-101B-BDE5-00AA0044DE52" \
 36	rename("RGB", "MSORGB") \
 37	rename("DocumentProperties", "MSODocumentProperties")
 38// [-or-]
 39//#import "C:\\Program Files\\Common Files\\Microsoft Shared\\OFFICE12\\MSO.DLL" \
 40//	rename("RGB", "MSORGB") \
 41//	rename("DocumentProperties", "MSODocumentProperties")
 42
 43using namespace Office;
 44
 45#import "libid:0002E157-0000-0000-C000-000000000046"
 46// [-or-]
 47//#import "C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA6\\VBE6EXT.OLB"
 48
 49using namespace VBIDE;
 50
 51#import "libid:91493440-5A91-11CF-8700-00AA0060263B" \
 52	rename("RGB", "VisioRGB")
 53// [-or-]
 54//#import "C:\\Program Files\\Microsoft Office\\Office12\\MSPPT.OLB" \
 55//	rename("RGB", "VisioRGB")
 56
 57#pragma endregion
 58
 59
 60//
 61//   FUNCTION: GetModuleDirectory(LPWSTR, DWORD);
 62//
 63//   PURPOSE: This is a helper function in this sample. It retrieves the 
 64//      fully-qualified path for the directory that contains the executable 
 65//      file of the current process. For example, "D:\Samples\".
 66//
 67//   PARAMETERS:
 68//      * pszDir - A pointer to a buffer that receives the fully-qualified 
 69//      path for the directory taht contains the executable file of the 
 70//      current process. If the length of the path is less than the size that 
 71//      the nSize parameter specifies, the function succeeds and the path is 
 72//      returned as a null-terminated string.
 73//      * nSize - The size of the lpFilename buffer, in characters.
 74//
 75//   RETURN VALUE: If the function succeeds, the return value is the length 
 76//      of the string that is copied to the buffer, in characters, not 
 77//      including the terminating null character. If the buffer is too small 
 78//      to hold the directory name, the function returns 0 and sets the last 
 79//      error to ERROR_INSUFFICIENT_BUFFER. If the function fails, the return 
 80//      value is 0 (zero). To get extended error information, call 
 81//      GetLastError.
 82//
 83DWORD GetModuleDirectory(LPWSTR pszDir, DWORD nSize);
 84
 85
 86//
 87//   FUNCTION: AutomatePowerPointByImport(LPVOID)
 88//
 89//   PURPOSE: Automate Microsoft PowerPoint using the #import directive and 
 90//      smart pointers.
 91// 
 92DWORD WINAPI AutomatePowerPointByImport(LPVOID lpParam)
 93{
 94	// Initializes the COM library on the current thread and identifies the 
 95	// concurrency model as single-thread apartment (STA). 
 96	// [-or-] CoInitialize(NULL);
 97	// [-or-] CoCreateInstance(NULL);
 98	CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
 99
100	try
101	{
102
103		/////////////////////////////////////////////////////////////////////
104		// Create the PowerPoint.Application COM object using the #import 
105		// directive and smart pointers.
106		// 
107
108		// Option 1) Create the object using the smart pointer's constructor
109		// 
110		// _ApplicationPtr is the original interface name, _Application, with a 
111		// "Ptr" suffix.
112		//PowerPoint::_ApplicationPtr spPpApp(
113		//	__uuidof(PowerPoint::Application)	// CLSID of the component
114		//	);
115
116		// Option 2) Create the object using the smart pointer's function,
117		// CreateInstance
118		PowerPoint::_ApplicationPtr spPpApp;
119		HRESULT hr = spPpApp.CreateInstance(__uuidof(PowerPoint::Application));
120		if (FAILED(hr))
121		{
122			wprintf(L"CreateInstance failed w/err 0x%08lx\n", hr);
123			return 1;
124		}
125
126		_putws(L"PowerPoint.Application is started");
127
128
129		/////////////////////////////////////////////////////////////////////
130		// Make PowerPoint invisible. (i.e. Application.Visible = 0)
131		// 
132
133		// By default PowerPoint is invisible, till you make it visible:
134		//spPpApp->put_Visible(Office::MsoTriState::msoFalse);
135
136
137		/////////////////////////////////////////////////////////////////////
138		// Create a new Presentation. (i.e. Application.Presentations.Add)
139		// 
140
141		PowerPoint::PresentationsPtr spPres = spPpApp->Presentations;
142		PowerPoint::_PresentationPtr spPre = spPres->Add(Office::msoTrue);
143
144		_putws(L"A new presentation is created");
145
146
147		/////////////////////////////////////////////////////////////////////
148		// Insert a new Slide and add some text to it.
149		// 
150
151		PowerPoint::SlidesPtr spSlides = spPre->Slides;
152
153		wprintf(L"The presentation currently has %ld slides\n", 
154			spSlides->Count);
155
156        // Insert a new slide
157		_putws(L"Insert a slide");
158		PowerPoint::_SlidePtr spSlide = spSlides->Add(1, 
159			PowerPoint::ppLayoutText);
160
161		// Add some texts to the slide
162        _putws(L"Add some texts");
163		PowerPoint::ShapesPtr spShapes = spSlide->Shapes;
164		PowerPoint::ShapePtr spShape = spShapes->Item((long)1);
165		PowerPoint::TextFramePtr spTxtFrame = spShape->TextFrame;
166		PowerPoint::TextRangePtr spTxtRange = spTxtFrame->TextRange;
167        spTxtRange->Text = _bstr_t(L"All-In-One Code Framework");
168
169
170		/////////////////////////////////////////////////////////////////////
171		// Save the presentation as a pptx file and close it.
172		// 
173
174        _putws(L"Save and close the presentation");
175
176		// Make the file name
177
178		// Get the directory of the current exe.
179		wchar_t szFileName[MAX_PATH];
180		if (!GetModuleDirectory(szFileName, ARRAYSIZE(szFileName)))
181		{
182			_putws(L"GetModuleDirectory failed");
183			return 1;
184		}
185
186		// Concat "Sample1.pptx" to the directory
187		wcsncat_s(szFileName, ARRAYSIZE(szFileName), L"Sample1.pptx", 12);
188
189		spPre->SaveAs(_bstr_t(szFileName), 
190			PowerPoint::ppSaveAsOpenXMLPresentation, Office::msoTriStateMixed);
191
192        spPre->Close();
193
194
195		/////////////////////////////////////////////////////////////////////
196		// Quit the PowerPoint application.
197		// 
198
199		_putws(L"Quit the PowerPoint application");
200		spPpApp->Quit();
201
202
203		/////////////////////////////////////////////////////////////////////
204		// Release the COM objects.
205		// 
206
207		// Releasing the references is not necessary for the smart pointers
208		// ...
209		// spPowerPointApp.Release();
210		// ...
211
212	}
213	catch (_com_error &err)
214	{
215		wprintf(L"PowerPoint throws the error: %s\n", err.ErrorMessage());
216		wprintf(L"Description: %s\n", (LPCWSTR) err.Description());
217	}
218
219	// Uninitialize COM for this thread
220	CoUninitialize();
221
222	return 0;
223}