热爱互联网

PowerPoint automation with C++

这段时间一直在看COM的东西,有了那么一点的认识,混了个眼熟,下次看到这种函数肯定是认识了。
这里有不错的讲解:http://www.cnblogs.com/zxjay/tag/COM/
下面这个也不错,不过其中代码有些错误需要改动才能通过编译:
http://blog.csdn.net/liwang112358/article/details/7364676

谷歌各种搜也没找到一个靠谱的COM控制Microsoft PPT放映的Demo,这篇文章是2002年的,现在的vs编译不过,不过很具参考价值:http://www.codeproject.com/Articles/2506/Automating-MS-Office-applications
这里有个小demo,用两种方式实现了ppt的自动创建,也可以参考:http://blogs.msdn.com/b/codefx/archive/2012/06/30/sample-of-june-30th-c-application-automates-microsoft-powerpoint.aspx

这里还有个讨论:http://social.msdn.microsoft.com/forums/office/en-US/971eeacb-9f1c-4c3f-a4cd-80b2fc86bf28/how-to-get-the-name-of-a-slide-in-the-edit-window

最后的绝招就是官方文档的,但是万恶的文档只有单纯的说明,并且还是针对VBA的,有些函数并没有给出都可以使用。当然绝大部分函数都可以凭着感觉使用的,哈哈。
http://msdn.microsoft.com/en-us/library/office/ff746846(v=office.14).aspx

下面是一个完整的控制台程序,自动操作Microsoft PowerPoint 2010,在 vs2010和vs2013下测试通过。
功能:首先获取活动的PPT窗口,然后跳转到第一张幻灯片,一张一张放,直到最后一张,然后再往前一张一张放,直到第一张,再往后… 循环。还可以根据官网文档实现更复杂的功能,比如:动态跳转到指定页,插入内容等等,还可以将此控制程序当做服务器来接收客户端(手机等)的指令,从而实现远程控制,这些都留着以后需要时再做了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#pragma region Includes
 
#include <stdio.h>
#include <Windows.h>
 
#pragma endregion
 
#pragma region Import the type libraries
#import "libid:2DF8D04C-5BFA-101B-BDE5-00AA0044DE52" \
	rename("RGB", "MSORGB") \
	rename("DocumentProperties", "MSODocumentProperties")
// [-or-]
//#import "C:\\Program Files\\Common Files\\Microsoft Shared\\OFFICE12\\MSO.DLL" \
//	rename("RGB", "MSORGB") \
//	rename("DocumentProperties", "MSODocumentProperties")
 
using namespace Office;
 
#import "libid:0002E157-0000-0000-C000-000000000046"
// [-or-]
//#import "C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA6\\VBE6EXT.OLB"
 
using namespace VBIDE;
 
#import "libid:91493440-5A91-11CF-8700-00AA0060263B" \
	rename("RGB", "VisioRGB")
// [-or-]
//#import "C:\\Program Files\\Microsoft Office\\Office12\\MSPPT.OLB" \
//	rename("RGB", "VisioRGB")
 
#pragma endregion
 
 
 
void AutomationPowerPointByCOMAPI()
{
	CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
 
	try
	{
		PowerPoint::_ApplicationPtr app;
		HRESULT hr = app.CreateInstance(__uuidof(PowerPoint::Application));
 
		if (FAILED(hr))
		{
			wprintf(L"CreateInstance failed w/err %x\n", hr);
			return;
		}
 
		_putws(L"PowerPoint.Application is started");
 
		PowerPoint::_PresentationPtr PresentationPtr = app->GetActivePresentation();
		PowerPoint::SlidesPtr SlidesPtr = PresentationPtr->GetSlides();
		PowerPoint::SlideShowSettingsPtr SlideShowSettingsPtr = PresentationPtr->GetSlideShowSettings();
 
		_putws(L"Start show slides");
		SlideShowSettingsPtr->Run();
 
		int iSlidesCount = SlidesPtr->GetCount();
		printf("Slides count: %d\n", iSlidesCount);
 
		PowerPoint::SlideShowWindowPtr SlideShowWindowPtr = PresentationPtr->GetSlideShowWindow();
		PowerPoint::SlideShowViewPtr SlideShowViewPtr = SlideShowWindowPtr->GetView();
 
 
		int i = 1;
		bool isNext = true;
		SlideShowViewPtr->First();
 
		while (true)
		{
			Sleep(1000);
			if (isNext)
			{
				SlideShowViewPtr->Next();
				++i;
			}
			else
			{
				SlideShowViewPtr->Previous();
				--i;
			}
 
			if (i == iSlidesCount)
			{
				isNext = false;
			}
			else if (i == 1)
			{
				isNext = true;
			}
		}
 
	}
	catch (_com_error &err)
	{
		wprintf(L"PowerPoint throws the err: %s\n", err.ErrorMessage());
		wprintf(L"Description: %s\n", (LPCWSTR)err.Description());
	}
 
	CoUninitialize();
	printf("hello world\n");
}
 
int main()
{
	AutomationPowerPointByCOMAPI();
	return 0;
}

Post a Comment

Your email is kept private. Required fields are marked *