Microsoft Office.NET 1.0Visual Studio .NET 2003.NET 1.1Visual Studio 2005.NET 2.0C# 2.0中级开发Visual StudioWindows.NETC#
自动化 MS PowerPoint 演示文稿






2.10/5 (4投票s)
本文将演示如何自动化 MS PowerPoint 演示文稿并从中获取内容。
引言
我一直在从事 MS Office 应用程序的自动化工作。 我已经看到,很容易找到有关 MS Word 自动化的信息,但很难找到有关其他 Office 应用程序(如 PowerPoint、Excel)自动化的信息。
所以我决定自己编写一篇并与大家分享信息。
Using the Code
PowerPoint 的自动化与 Word 的自动化相同。 创建一个新项目,然后在解决方案资源管理器中右键单击“引用”,然后选择“**添加引用…**”。 当“添加引用”窗口出现时,选择“**COM**”选项卡。 这将列出机器上所有可用的组件名称。 由于我们将使用 MS PowerPoint,我们将向下滚动,直到找到:**Microsoft Power Point 对象库**。
以下代码将帮助您理解其余的自动化过程
public string presentationExtract(string path)
{
int slideNumber = 0;
string heading = null;
PowerPoint.Application PPTApp = null;
PowerPoint.Presentation PPTPre = null;
bool errorFlag = false;
StringBuilder sb = new StringBuilder("");
string wordArt = null;
try
{
PPTApp = new PowerPoint.Application();
PPTPre = PPTApp.Presentations.Open(path, Microsoft.Office.Core.MsoTriState.msoTrue,
Microsoft.Office.Core.MsoTriState.msoTriStateMixed, 0);
foreach (PowerPoint.Slide objSlide in PPTPre.Slides)
{
slideNumber = objSlide.SlideNumber;
heading = "\r\n\r\n" + " PRESENTATION SLIDE " +
slideNumber + " " + "\r\n\r\n";
sb.Append(heading);
foreach (PowerPoint.Shape objShape in objSlide.Shapes)
{
try
{
wordArt = objShape.TextEffect.Text.ToString();
sb.Append("\r\n" + wordArt + "\r\n");
}
catch (System.Exception wordartException)
{
string temp = wordartException.Message.ToString();
}
try
{
sb.Append("\r\n" + objShape.TextFrame.TextRange.Text + "\r\n");
}
catch (System.Exception textException)
{
string temp = textException.Message.ToString();
}
}
}
}
catch (System.Exception error)
{
string temp = error.Message.ToString();
errorFlag = true;
}
finally
{
PPTPre.Close();
PPTApp.Quit();
}
if (!errorFlag)
{
return (newContent);
}
else
return ("");
}
关注点
我想在这里提到一件事:我使用了 Office XP COM 对象,它可以与 MS Office 2003 和 2007 一起使用。
祝你好运!