Origin C PowerPoint access points and six main steps:
1. Create a PowerPoint application object, or get the current open PowerPoint application.
Origin C by CreateObject
creating function.
Object ppt;
// 新建一个PowerPoint应用
try {
ppt = CreateObject("powerpoint.application");
} catch (int err) {
printf("新建PowerPoint出错!");
}
if (ppt == NULL) {
printf("新建PowerPoint出错!");
return;
}
And get an open PowerPoint application object through GetActiveObject
to achieve.
Object ppt;
// 获取已经打开的PowerPoint应用
try {
ppt = GetActiveObject("powerpoint.application", false);
} catch(int err) {
printf("获取打开的PowerPoint出错!");
}
if (ppt == NULL) {
printf("获取打开的PowerPoint出错!");
return;
}
2. Create a demo or obtain objects, opens the PowerPoint file. You can open a PowerPoint file, you can also get open PowerPoint files.
To open PowerPoint files by specifying a path.
// 打开一个PowerPoint文件
Object presentation;
// PowerPoint文件的全路径
string strTemplate = GetOriginPath(ORIGIN_PATH_SYSTEM) + "Samples\\Import and Export\\Column_Plots_in_Sample_OPJ.pptx";
presentation = ppt.Presentations.Open(strTemplate);
if (presentation == NULL) {
printf("打开文件失败!");
return;
}
You can also pick up already open PowerPoint files.
Object presentation;
int numPres = ppt.Presentations.Count; // 当前PowerPoint应用打开的所有演示文件
if (numPres < 1) { // 如果没有文件打开,新加一个
presentation = ppt.Presentations.Add(1); // 新加一个文件
} else {
presentation = ppt.Presentations(numPres); // 拿到最后一个打开的文件
}
if (presentation == NULL) {
printf("打开演示文件失败!");
return;
}
3. Increase or get slide PowerPoint file.
int numSlide = presentation.Slides.Count; // 一共有多少张幻灯片
Object slide;
if (numSlide < 1) { // 如果一张幻灯片都还没有,就新加一张
slide = presentation.Slides.Add(1, 12); // ppLayoutBlank == 12, 新增一张空幻灯片
} else {
slide = presentation.Slides(numSlide-1); // 倒数第二张幻灯片
}
// 插入一张幻灯片,用前一张作为模板
Object slide2;
slide2 = slide.Duplicate();
4. The slide can then be used to manipulate the object.
Here is the copy and paste Origin in the Figure to PowerPoint.
string strGraphPageName = "Graph1"; // 要保证有Graph1这个图
GraphPage gp(strGraphPageName);
if (!gp) {
printf("粘贴失败!");
return;
}
gp.LT_execute("Clipboard %H"); // 复制Origin的图到剪贴板
Object shapes;
shapes = slide.Shapes;
if (shapes == NULL) {
printf("粘贴失败!");
return;
}
shapes.PasteSpecial(3); // ppPasteMetafilePicture == 3,粘贴到PowerPoint
5. Save the PowerPoint file.
presentation.SaveAs("D:\\test.pptx");
6. Exit PowerPoint.
ppt.Quit();
Origin also provides a built-in class PowerPoint operation, can include headers PowerPoint.h
, specific classes and methods can be opened directly reference the file header.
#include <..\originlab\PowerPoint.h>
Github can download the source code to: https://github.com/gkimeeq/OriginAdvancedApplication .