PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms 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. #pragma region Includes
  26. #include <stdio.h>
  27. #include <windows.h>
  28. #include "Solution1.h"
  29. #pragma endregion
  30. #pragma region Import the type libraries
  31. #import "libid:2DF8D04C-5BFA-101B-BDE5-00AA0044DE52" \
  32. rename("RGB", "MSORGB") \
  33. rename("DocumentProperties", "MSODocumentProperties")
  34. // [-or-]
  35. //#import "C:\\Program Files\\Common Files\\Microsoft Shared\\OFFICE12\\MSO.DLL" \
  36. // rename("RGB", "MSORGB") \
  37. // rename("DocumentProperties", "MSODocumentProperties")
  38. using namespace Office;
  39. #import "libid:0002E157-0000-0000-C000-000000000046"
  40. // [-or-]
  41. //#import "C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA6\\VBE6EXT.OLB"
  42. using namespace VBIDE;
  43. #import "libid:91493440-5A91-11CF-8700-00AA0060263B" \
  44. rename("RGB", "VisioRGB")
  45. // [-or-]
  46. //#import "C:\\Program Files\\Microsoft Office\\Office12\\MSPPT.OLB" \
  47. // rename("RGB", "VisioRGB")
  48. #pragma endregion
  49. //
  50. // FUNCTION: GetModuleDirectory(LPWSTR, DWORD);
  51. //
  52. // PURPOSE: This is a helper function in this sample. It retrieves the
  53. // fully-qualified path for the directory that contains the executable
  54. // file of the current process. For example, "D:\Samples\".
  55. //
  56. // PARAMETERS:
  57. // * pszDir - A pointer to a buffer that receives the fully-qualified
  58. // path for the directory taht contains the executable file of the
  59. // current process. If the length of the path is less than the size that
  60. // the nSize parameter specifies, the function succeeds and the path is
  61. // returned as a null-terminated string.
  62. // * nSize - The size of the lpFilename buffer, in characters.
  63. //
  64. // RETURN VALUE: If the function succeeds, the return value is the length
  65. // of the string that is copied to the buffer, in characters, not
  66. // including the terminating null character. If the buffer is too small
  67. // to hold the directory name, the function returns 0 and sets the last
  68. // error to ERROR_INSUFFICIENT_BUFFER. If the function fails, the return
  69. // value is 0 (zero). To get extended error information, call
  70. // GetLastError.
  71. //
  72. DWORD GetModuleDirectory(LPWSTR pszDir, DWORD nSize);
  73. //
  74. // FUNCTION: AutomatePowerPointByImport(LPVOID)
  75. //
  76. // PURPOSE: Automate Microsoft PowerPoint using the #import directive and
  77. // smart pointers.
  78. //
  79. DWORD WINAPI AutomatePowerPointByImport(LPVOID lpParam)
  80. {
  81. // Initializes the COM library on the current thread and identifies the
  82. // concurrency model as single-thread apartment (STA).
  83. // [-or-] CoInitialize(NULL);
  84. // [-or-] CoCreateInstance(NULL);
  85. CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  86. try
  87. {
  88. /////////////////////////////////////////////////////////////////////
  89. // Create the PowerPoint.Application COM object using the #import
  90. // directive and smart pointers.
  91. //
  92. // Option 1) Create the object using the smart pointer's constructor
  93. //
  94. // _ApplicationPtr is the original interface name, _Application, with a
  95. // "Ptr" suffix.
  96. //PowerPoint::_ApplicationPtr spPpApp(
  97. // __uuidof(PowerPoint::Application) // CLSID of the component
  98. // );
  99. // Option 2) Create the object using the smart pointer's function,
  100. // CreateInstance
  101. PowerPoint::_ApplicationPtr spPpApp;
  102. HRESULT hr = spPpApp.CreateInstance(__uuidof(PowerPoint::Application));
  103. if (FAILED(hr))
  104. {
  105. wprintf(L"CreateInstance failed w/err 0x%08lx\n", hr);
  106. return 1;
  107. }
  108. _putws(L"PowerPoint.Application is started");
  109. /////////////////////////////////////////////////////////////////////
  110. // Make PowerPoint invisible. (i.e. Application.Visible = 0)
  111. //
  112. // By default PowerPoint is invisible, till you make it visible:
  113. //spPpApp->put_Visible(Office::MsoTriState::msoFalse);
  114. /////////////////////////////////////////////////////////////////////
  115. // Create a new Presentation. (i.e. Application.Presentations.Add)
  116. //
  117. PowerPoint::PresentationsPtr spPres = spPpApp->Presentations;
  118. PowerPoint::_PresentationPtr spPre = spPres->Add(Office::msoTrue);
  119. _putws(L"A new presentation is created");
  120. /////////////////////////////////////////////////////////////////////
  121. // Insert a new Slide and add some text to it.
  122. //
  123. PowerPoint::SlidesPtr spSlides = spPre->Slides;
  124. wprintf(L"The presentation currently has %ld slides\n",
  125. spSlides->Count);
  126. // Insert a new slide
  127. _putws(L"Insert a slide");
  128. PowerPoint::_SlidePtr spSlide = spSlides->Add(1,
  129. PowerPoint::ppLayoutText);
  130. // Add some texts to the slide
  131. _putws(L"Add some texts");
  132. PowerPoint::ShapesPtr spShapes = spSlide->Shapes;
  133. PowerPoint::ShapePtr spShape = spShapes->Item((long)1);
  134. PowerPoint::TextFramePtr spTxtFrame = spShape->TextFrame;
  135. PowerPoint::TextRangePtr spTxtRange = spTxtFrame->TextRange;
  136. spTxtRange->Text = _bstr_t(L"All-In-One Code Framework");
  137. /////////////////////////////////////////////////////////////////////
  138. // Save the presentation as a pptx file and close it.
  139. //
  140. _putws(L"Save and close the presentation");
  141. // Make the file name
  142. // Get the directory of the current exe.
  143. wchar_t szFileName[MAX_PATH];
  144. if (!GetModuleDirectory(szFileName, ARRAYSIZE(szFileName)))
  145. {
  146. _putws(L"GetModuleDirectory failed");
  147. return 1;
  148. }
  149. // Concat "Sample1.pptx" to the directory
  150. wcsncat_s(szFileName, ARRAYSIZE(szFileName), L"Sample1.pptx", 12);
  151. spPre->SaveAs(_bstr_t(szFileName),
  152. PowerPoint::ppSaveAsOpenXMLPresentation, Office::msoTriStateMixed);
  153. spPre->Close();
  154. /////////////////////////////////////////////////////////////////////
  155. // Quit the PowerPoint application.
  156. //
  157. _putws(L"Quit the PowerPoint application");
  158. spPpApp->Quit();
  159. /////////////////////////////////////////////////////////////////////
  160. // Release the COM objects.
  161. //
  162. // Releasing the references is not necessary for the smart pointers
  163. // ...
  164. // spPowerPointApp.Release();
  165. // ...
  166. }
  167. catch (_com_error &err)
  168. {
  169. wprintf(L"PowerPoint throws the error: %s\n", err.ErrorMessage());
  170. wprintf(L"Description: %s\n", (LPCWSTR) err.Description());
  171. }
  172. // Uninitialize COM for this thread
  173. CoUninitialize();
  174. return 0;
  175. }