microsoft office powerpoibt automation of secondary development

background

First of office products powerpoint supports secondary development, secondary development here does not mean some of their controls embedded in powerpoint products, but some simple automation of control (state into the show, Previous, Next etc.), do not make any changes to powerpoint product. microsoft official website provides a lot of demo on automation, these demo are available in https://code.msdn.microsoft.com/site/search?query=&f%5B0%5D.Value=&f%5B0%5D.Type= SearchText & ac = 4 search keywords download.

Registry

The reason why the registry mentioned here, is because the development phase usually powerpoint install multiple versions of these different versions of the software at the time of unloading, not necessarily to clean up the registry, but we need a program based on the version number on the registry find the corresponding pia, which is packaged assemblies, if not clean up the registry information is present, it will lead to code does not work.

powerpoint Activation Wizard or protected mode

Normal ideas of automation should be: open powerpoint software => Open the need to automate control of ppt => enter show mode. But because there is no active powerpoint software, or read-only file, it will lead to automatic control failure, bypass this step approach is the last step directly => Enter show mode. This avoids the previous two operations caused by uncontrollable.

Code

Step1. New in visual studio in a console-based application .Net framwork of.

Step2. Add the required dll references, generally requires "Microsoft.Office.Interop.PowerPoint.dll 14.00" The better compatibility, there is an "office" is. The dll can be found in the office of the installation directory.

Step3: add specific automation code

// 拿到app的实例,假设app都没有运行
var application = new Application()

// 这里可以直接打开powerpoint,不过我们并不这么操作
//application.Visible = MsoTriState.msoTrue;

var pres = application.Presentations;

// 打开ppt
var file = pres.Open(@"c://aa/a.pptx", OF.MsoTriState.msoFalse, 
OF.MsoTriState.msoFalse, OF.MsoTriState.msoTrue);

// 自动进入播放状态
SlideShowSettings ss = file.SlideShowSettings;

ss.StartingSlide = 1;

ss.EndingSlide = file.Slides.Count;

ss.Run();


Thread.Sleep(500);

// 播放下一页
application.SlideShowWindows[1].View.Next();

Thread.Sleep(500);
// 推出放映
application.SlideShowWindows[1].View.Exit();

Automated control position here is over, of course, if you want to listen play events, you can add your own monitor.

Gets powerpoint already running instance

Here we need to understand a concept, ROT (running object table), where the main memory is running the object.

// 拿到运行中的对象,如果ppt没有运行,则会直接报错
var pptApplication = Marshal.GetActiveObject(applicationName) as Application;

Then there is some common event

 // 开始放映
pptApplication.SlideShowBegin += PptApplication_SlideShowBegin;
// 放映结束
pptApplication.SlideShowEnd += PptApplication_SlideShowEnd;
// 全部关闭
pptApplication.PresentationCloseFinal += 
PptApplication_PresentationCloseFinal;

Guess you like

Origin www.cnblogs.com/Sir-Lin/p/11869043.html