本篇分享对于Power Point中一些命令的操作和对于一些比较常用对象、特殊对象的添加功能。
对于Power Point命令操作:
有了前一篇《
[转]VSTO Office二次开发RibbonX代码结构
》的了解,就可以尝试现实自己的RibbonX的相关元素的操作了,这里提供简单的小示例:
1.创建外接程序。
创建一个PPT的外接程序,在《
VSTO Office二次开发对PowerPoint功能简单测试
》随笔中有介绍,如何创建一个简单的PPT外接程序。
2.创建功能区(XML)。
在项目右键单击添加,弹出“添加新项”,添加“功能区(XML)”,以XML的形式创建功能区。
在Ribbon1.xml添加如下代码,定义三个回调事件"MyCopy"\"MyCut"\"GetInfo":
<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="Ribbon_Load">
<commands>
<!--PPT中的文件保存功能不能使用-->
<command idMso="FileSave" enabled="false"/>
<!--"MyCut"扩展PPT中"剪切"命令的响应事件-->
<command idMso="Cut" enabled="true" onAction="MyCut"/>
<command idMso="Copy" enabled="true" onAction="MyCopy"/>
</commands>
<ribbon>
<tab idMso="TabAddIns">
<group id="MyGroup"
label="My Group">
<button id="MyButton" label="MyButton" onAction="GetInfo"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
3.回调函数编写。
在Ribbon1.cs文件中添加对于回调函数的处理代码:
/// <summary>
/// MyButton的回调方法
/// </summary>
/// <param name="control"></param>
public void GetInfo(Office.IRibbonControl control)
MessageBox.Show("Released");
/// <summary>
/// MyCopy的回调方法
/// </summary>
/// <param name="control">触发控件</param>
/// <param name="cancelDefault">是否取消功能</param>
public void MyCopy(Office.IRibbonControl control, ref bool cancelDefault)
bool isSuccess = IsSuccess();
//如果cancelDefault返回false,则取消该操作
cancelDefault = isSuccess;
/// <summary>
/// 要处理的业务
/// </summary>
/// <returns></returns>
private bool IsSuccess()
return false;
4.关联外接程序。
前几步把Ribbon设计完毕,现在与Power Point外接程序进行关联:(重写RequestService方法)
private Ribbon1 ribbon;//重新定义Ribbon1
/// <summary>
/// 重写RequestService方法
/// </summary>
/// <param name="serviceGuid"></param>
/// <returns></returns>
protected override object RequestService(Guid serviceGuid)
if (serviceGuid == typeof(Office.IRibbonExtensibility).GUID)
if (ribbon == null)
ribbon = new Ribbon1();
return ribbon;
return base.RequestService(serviceGuid);
5.功能测试。
F5运行程序,简单测试。对于“文件保存”、“剪切”、“复制”操作的测试。
对于操作的RibbonX的Office 2010所有命令(Office2010ControlIDs.exe也有对于2007支持的命令):
参见:http://social.msdn.microsoft.com/Forums/pl/worddev/thread/337946b2-edc4-4f40-bb45-1babf58a5e7e
下载:http://www.microsoft.com/download/en/details.aspx?id=6627
对于RibbonX进行对Office程序的XML编程很不错。几个可参考资源:
使用 Open XML 文件格式自定义 Office Fluent 功能区
利用您自己的功能区选项卡和控件扩展 2007 Office System
细品RibbonX(12):使用XML Notepad自定义功能区
http://www.360doc.com/content/09/1110/11/406571_8725398.shtml
对于PowerPoint添加对象:
微软也提供了一些调用接口:
《Shapes.AddPicture 方法 》http://msdn.microsoft.com/zh-cn/library/office/jj735114.aspx
《OLE 程序标识符 (PowerPoint)》http://msdn.microsoft.com/zh-cn/library/office/ff746158.aspx
常用对象添加调用代码:
/// <summary>
/// 添加常用对象
/// 通过OLE添加对象,支持网络文件访问
/// 添加公式:Equation.3
/// 添加图表:MSGraph.Chart
/// </summary>
/// <param name="slide">幻灯片</param>
/// <param name="filePath">文件路径</param>
/// <param name="left">居左位置</param>
/// <param name="top">居右位置</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
#region 添加对象操作
//添加图片
private void AddADPicture(PowerPoint.Slide slide, string filePath, float left, float top, float width, float height)
PowerPoint.Shape pic;
pic = slide.Shapes.AddPicture(filePath, Office.MsoTriState.msoFalse, Office.MsoTriState.msoTrue,left,top,width,height);
pic.Name = "Picture";
pic.Tags.Add("Name", "Value");//或用tag标签携带数据
//添加媒体文件
private void AddMedia(PowerPoint.Slide slide, string filePath, float left, float top, float width, float height)
PowerPoint.Shape media;
media = slide.Shapes.AddMediaObject(filePath,left,top,width,height);
media.Name = "Media";
//添加任意对象,但需要激活操作
private void AddOLEDPath(PowerPoint.Slide slide, string filePath, float left, float top, float width, float height)
PowerPoint.Shape oledFile;
oledFile = slide.Shapes.AddOLEObject(left,top,height,width,FileName: filePath, DisplayAsIcon: Office.MsoTriState.msoTrue);//, DisplayAsIcon: Office.MsoTriState.msoTrue
oledFile.Name = filePath;
//添加Flash播放器,需要本机上安装Flash播放插件
private void AddFlashObj(PowerPoint.Slide slide, string fileName, float left, float top, float width, float height)
object oleControl = slide.Shapes.AddOLEObject(left, top, width, height, "ShockwaveFlash.ShockwaveFlash", "",
Office.MsoTriState.msoFalse, "", 0, "", Office.MsoTriState.msoFalse).OLEFormat.Object;
Type oleControlType = oleControl.GetType(); /* 设置flash播放属性 */
oleControlType.InvokeMember("EmbedMovie", BindingFlags.SetProperty, null, oleControl, new object[] { true });
oleControlType.InvokeMember("Playing", BindingFlags.SetProperty, null, oleControl, new object[] { true });
oleControlType.InvokeMember("Movie", BindingFlags.SetProperty, null, oleControl, new object[] { fileName });// 设置Flash文件路径
oleControlType.InvokeMember("Scale", BindingFlags.SetProperty, null, oleControl, new object[] { "ExactFit" });//设置显示比例为:严格匹配
oleControlType.InvokeMember("ScaleMode", BindingFlags.SetProperty, null, oleControl, new object[] { 2 });
//添加3D模型播放器,需要本机上安装BS Contact插件
private void AddBSContactObj(PowerPoint.Slide slide, string fileName, float left, float top, float width, float height)
object oleControl = slide.Shapes.AddOLEObject(left, top, width, height, "BSContact.BSContact", "",
Office.MsoTriState.msoFalse, "", 0, "", Office.MsoTriState.msoFalse).OLEFormat.Object;
Type oleControlType = oleControl.GetType(); /* 设置BSContact播放属性 */
oleControlType.InvokeMember("Enabled", BindingFlags.SetProperty, null, oleControl, new object[] { true });
oleControlType.InvokeMember("url", BindingFlags.SetProperty, null, oleControl, new object[] { fileName });// 设置BSContact文件路径
oleControlType.InvokeMember("walkSpeed", BindingFlags.SetProperty, null, oleControl, new object[] { 0 });
oleControlType.InvokeMember("animateAllViewpoints", BindingFlags.SetProperty, null, oleControl, new object[] { false });
//添加图片,以OLE对象形式
private void AddOlePicture(PowerPoint.Slide slide, string fileName, float left, float top, float width, float height)
object oleControl = slide.Shapes.AddOLEObject(left, top, width, height, "Forms.Image.1", "",
Office.MsoTriState.msoFalse, "", 0, "", Office.MsoTriState.msoFalse).OLEFormat.Object;
Type oleControlType = oleControl.GetType();
oleControlType.InvokeMember("Enabled", BindingFlags.SetProperty, null, oleControl, new object[] { true });
Bitmap bitmap = new Bitmap(fileName);
stdole.StdPicture pic = (stdole.StdPicture)ImageConverter2.ImageToIpicture(bitmap);
oleControlType.InvokeMember("Picture", BindingFlags.SetProperty, null, oleControl, new object[] { pic });
//添加Windows Media Player播放器,本机安装Windows Media Player(插件)
private void AddOleMedia(PowerPoint.Slide slide, string fileName, float left, float top, float width, float height)
//MediaPlayer.MediaPlayer.1或WMPlayer.OCX.7
object oleControl = slide.Shapes.AddOLEObject(left, top, width, height, "WMPlayer.OCX.7", "",
Office.MsoTriState.msoFalse, "", 0, "", Office.MsoTriState.msoFalse).OLEFormat.Object;
Type oleControlType = oleControl.GetType();
oleControlType.InvokeMember("Enabled", BindingFlags.SetProperty, null, oleControl, new object[] { true });
oleControlType.InvokeMember("Url", BindingFlags.SetProperty, null, oleControl, new object[] {fileName });
//添加Form窗体,窗体中添加Image控件,单击弹出"PPT"信息提示
//命名引用:using MF = Microsoft.Vbe.Interop.Forms;
private void AddOleForm(PowerPoint.Slide slide, string fileName, float left, float top, float width, float height)
var oleControl = slide.Shapes.AddOLEObject(left, top, width, height, "Forms.Frame.1", "",
Office.MsoTriState.msoFalse, "", 0, "", Office.MsoTriState.msoFalse);
var obj = oleControl.OLEFormat.Object;
oleControl.Name = "Frame";
PowerPoint.OLEFormat oleF = slide.Shapes.Range("Frame").OLEFormat;
MF.Frame frm1 = (MF.Frame)oleF.Object;
frm1.Caption = "PP";
MF.Image image = (MF.Image)frm1.Controls.Add("Forms.Image.1");
image.Click += new MF.ImageEvents_ClickEventHandler(image_Click);
Bitmap bitmap = new Bitmap(fileName);
image.Picture = (stdole.StdPicture)ImageConverter2.ImageToIpicture(bitmap);
void image_Click()
MessageBox.Show("PPT");
#endregion
其中,图片转化类,将StdPicture转换为Bitmap:
public class ImageConverter2 : System.Windows.Forms.AxHost
public ImageConverter2()
: base("59EE46BA-677D-4d20-BF10-8D8067CB8B33")
public static stdole.IPictureDisp ImageToIpicture(System.Drawing.Image image)
return (stdole.IPictureDisp)ImageConverter2.GetIPictureDispFromPicture(image);
public static System.Drawing.Image IPictureToImage(stdole.StdPicture picture)
return ImageConverter2.GetPictureFromIPicture(picture);
也可以添加Word\Excel\PPT《VSTO Office二次开发对PPT自定义任务窗格测试》,发现通过添加OLE对象使用ClassName标识的形式,可以添加本机安装的很多Com组件,在PPT中插入的ActiveX组件基本都可以通过OLE对象通过程序添加到PPT中。
本文转自SanMaoSpace博客园博客,原文链接:http://www.cnblogs.com/SanMaoSpace/archive/2013/03/01/2939498.html,如需转载请自行联系原作者