-
2009-10-25
学习写作一个PPT插件(入门级) - [PPT插件设计]
学习写作一个PPT插件
在PowerPoint2003中,当你插入一个Shape(图形)时,PowerPoint会傻乎乎地给Shape命名为诸如:矩形1,矩形2……,看着这一大堆一模一样的孪生兄弟,我们都搞不清楚谁是谁了,即使是微软自己也一定为搞不清楚而苦恼,因此在PowerPoint2007中才提供了一个叫做“选择窗格”的东西,不过这东西还是有点问题:它不能用拖动的方式来调整对象的叠放顺序。
下面我们就分系列来写作一个能在PowerPoint2003中重命名对象、显示/隐藏对象、快速调整对象叠放顺序的插件,希望朋友们能通过这样一个实例的操作快速地入门PPT插件的开发。
一、给所选对象重命名
(1)建立一个PPT文档
(2)按Alt+F11打开VBA编程环境。
(3)选择“插入”->“类模块”,并把这个类模块命名为“clsApp”
(4)在类模块中输入下列代码:
Public WithEvents App As Application '声明一个响应事件的PPT程序对象App
Public ActiveObj As ObjectPrivate Sub App_SlideSelectionChanged(ByVal SldRange As SlideRange)
If ActivePresentation.ReadOnly Then Exit Sub '如果演示文稿只读则退出。
If SldRange.Count = 1 Then '如果没有选择多张幻灯片,这样就可以对所选择的幻灯片进行重命名
cbMenu.Controls("Edit").Text = SldRange.Name
Set ActiveObj = SldRange
Else
cbMenu.Controls("Edit").Text = ""
Set ActiveObj = Nothing
End If
End SubPrivate Sub App_WindowSelectionChange(ByVal Sel As Selection)
If ActivePresentation.ReadOnly Then Exit Sub
Select Case Sel.Type '判断选区类型
Case ppSelectionText '选择的是文本框
cbMenu.Controls("Edit").Text = Sel.ShapeRange.Name
Set ActiveObj = Sel.ShapeRange
Case ppSelectionShapes '选择的是Shape
If Sel.ShapeRange.Count = 1 Then '只能选一个Shape,否则出错
cbMenu.Controls("Edit").Text = Sel.ShapeRange.Name
Set ActiveObj = Sel.ShapeRange
Else
cbMenu.Controls("Edit").Text = ""
Set ActiveObj = Nothing
End If
Case ppSelectionNone '什么也没有选
cbMenu.Controls("Edit").Text = ""
Set ActiveObj = Nothing
End Select
End Sub(5)插入一个模块并命名为mdApp,输入下列代码:
Public clsApp As New clsApp '声明刚才插入的类模块的实例clsapp
Public cbMenu As CommandBar '声明一个命令栏对象'下面是两个自动运行的宏,这个宏只在PPA中才有效。
Sub Auto_Open()
Set clsApp.App = Application '把当前的程序实例与类中的App对象进行关联
CreateMenu '建立工具栏
End SubSub Auto_Close()
On Error Resume Next
CommandBars("Name Tools").Delete '退出程序时删除建立的命令栏
End
End Sub'###############################
'建立命令栏的子过程
'###############################
Sub CreateMenu()
On Error Resume Next
CommandBars("Name Tools").Delete
Err.Clear
Dim cbCtl As CommandBarControl '声明一个命令栏上的控件Set cbMenu = CommandBars.Add("Name Tools") '增加一个叫做"Name Tools"的命令栏
With cbMenu.Controls.Add(msoControlEdit) '在这个命令栏上增加一个Edit控件
.Caption = "Edit"
.OnAction = "Change" '响应动作:Change
End With
cbMenu.Visible = True '让命令栏可见
End Sub'#########################################
'给所选择的对象重命名的子过程
'#########################################
Sub Change()
On Error Resume Next
With cbMenu.Controls("Edit")
If .Text = "" Then: .Text = clsApp.ActiveObj.Name '把所选择对象的名字显示在文框中
clsApp.ActiveObj.Name = .Text '输入名字并按下回车给对象重命名
Select Case Err '捕捉错误信息并进行处理
Case 70
ActiveWindow.Selection.SlideRange.Shapes(.Text).Select
Case -2147188160
ActivePresentation.Slides(.Text).Select
End Select
End With
End Sub(6)在PowerPoint中选择“文件”->“另存为”,“保存类型”选择“PowerPoint 加载宏(*.ppa)”,一个给对象重命名的插件就做好了。
(7)测试效果,在PowerPoint中,选择“工具”->“加载宏”,然后浏览至你刚才保存的文件就可以加载我们刚才编写的插件了。
示例插件下载:http://www.brsbox.com/filebox/down/fc/b68fb4ee97cce3b08c5255d58b872410
下一讲,我将改写上面的代码,把它放入到一个窗体中,从而实现命名的批量功能和对象的显示/隐藏功能。
-
2009-10-25
怎样使用VB.net处理PPT2003事件 - [PPT插件设计]
使用VB.net处理PPT2003事件
构建一个事件句柄
你能够在VB.net中使用下面方法之一构建一个事件句柄。采用哪种方法取决于你打算怎样用一个事件去关联事件句柄。
方法1
典型的做法是使用WithEvents关键字建立一个事件句柄。
方法2
VB.net提供了另一种方式处理事件,你可以使用AddHandler和RemoveHandler语句动态地开始和停止指定事件的句柄。注意: 如果你使用WithEvents关键字构建了事件句柄,那么你可能会接收到一个System.Reflection.TargetInvocationException异常,因此
,最好采用AddHandler来构建事件句柄。
建立VB.net自动客户端处理PPT事件
下面将分步告诉你怎样使用AddHandler语句处理PPT事件:
按下列步骤建立一个VB工程:
(1)开始Microsoft Visual Studio .NET .
(2)在文件菜单中,单击新建然后单击工程。
(3)在工程类型列表中,单击VB工程,在模版列表中,单击Windows应用程序。
(4)把工程名命名为PowerPointEvents然后单击“确定”。增加PPT对象库和Graph对象库的引用:
(1)在工程菜单中,单击增加引用。
(2)在COM选项中,单击Microsoft PowerPoint 11.0 Object Library 然后选择。.
(3)在COM选项中,单击Microsoft Graph 11.0 Object Library 然后选择。
(4)然后单击“确定”。
(5)双击窗体,打开代码窗口。
(6)在Form1.vb的顶部,增加下面的代码:
Imports PowerPoint = Microsoft.Office.Interop.PowerPoint
Imports Office = Microsoft.Office.Core
Imports Graph = Microsoft.Office.Interop.Graph(7)在视图菜单中,单击Designer(设计).
(8)在视图菜单中,单击“工具栏”,然后增加一个按钮给Form1.
(9)双击Button1.,打开 Button1_Click事件。
(10)在Button1_Click的前面插入下面代码:
Dim oApp As PowerPoint.Application
Dim oPres As PowerPoint.Presentation(11)把下列代码增加给Button1_Click事件。
Const sTemplate = "C:\Program Files\Microsoft Office\Templates\Presentation Designs\Orbit.pot"
' 如果前面 模版无效,请改变上面的内容。
Const sVideo = "C:\WINDOWS\system32\oobe\images\intro.wmv"Dim oPresentations As PowerPoint.Presentations
Dim oSlides As PowerPoint.Slides
Dim oSlide As PowerPoint.Slide
Dim oShapes As PowerPoint.Shapes
Dim oShape As PowerPoint.Shape
Dim oMovie As PowerPoint.Shape
Dim oAnimationSettings As PowerPoint.AnimationSettings
Dim oPlaySettings As PowerPoint.PlaySettings
Dim oTextFrame As PowerPoint.TextFrame
Dim oTextRange As PowerPoint.TextRange
Dim oFont As PowerPoint.Font
Dim oOLEFormat As PowerPoint.OLEFormat
Dim oShadow As PowerPoint.ShadowFormat
Dim oForeColor As PowerPoint.ColorFormat
Dim oRange As PowerPoint.SlideRange
Dim oSlideShowTransition As PowerPoint.SlideShowTransition'启动PowerPoint然后把其窗口最小化。
oApp = New PowerPoint.Application'增加事件句柄
AddHandler oApp.SlideShowBegin, AddressOf oApp_SlideShowBegin
AddHandler oApp.SlideShowNextSlide, AddressOf oApp_SlideShowNextSlide
AddHandler oApp.PresentationClose, AddressOf oApp_PresentationCloseoApp.Visible = True
oApp.WindowState = PowerPoint.PpWindowState.ppWindowMinimized'用指定的模版建立一个新演示文稿。
oPresentations = oApp.Presentations
oPres = oPresentations.Open(sTemplate, , , True)'建立幻灯片1
'增加文本到该幻灯片,设置字体然后插入一个影片。
oSlides = oPres.Slides
oSlide = oSlides.Add(1, PowerPoint.PpSlideLayout.ppLayoutTitleOnly)
oShapes = oSlide.Shapes
oShape = oShapes.Item(1)
oTextFrame = oShape.TextFrame
oTextRange = oTextFrame.TextRange
oTextRange.Text = "一个示例演示文稿"
oFont = oTextRange.Font
oFont.Name = "黑体"
oFont.Size = 48oMovie = oShapes.AddMediaObject(sVideo, 150, 150, 500, 350)
oAnimationSettings = oMovie.AnimationSettings
oPlaySettings = oAnimationSettings.PlaySettings
oPlaySettings.PlayOnEntry = True
oPlaySettings.HideWhileNotPlaying = True'释放对象
NAR(oPlaySettings)
NAR(oAnimationSettings)
NAR(oMovie)
NAR(oFont)
NAR(oTextRange)
NAR(oTextFrame)
NAR(oShape)
NAR(oShapes)
NAR(oSlide)
NAR(oSlides)'建立幻灯片2.
'增加文本到该幻灯片标题并格式化文本,增加一个 chart并把其类型设置为三维柱状样式
oSlides = oPres.Slides
oSlide = oSlides.Add(2, PowerPoint.PpSlideLayout.ppLayoutTitleOnly)
oShapes = oSlide.Shapes
oShape = oShapes.Item(1)
oTextFrame = oShape.TextFrame
oTextRange = oTextFrame.TextRange
oTextRange.Text = "My chart"
oFont = oTextRange.Font
oFont.Name = "Comic Sans MS"
oFont.Size = 48Dim oChart As Graph.Chart
oShape = oShapes.AddOLEObject(150, 150, 480, 320, "MSGraph.Chart.8")
oOLEFormat = oShape.OLEFormat
oChart = oOLEFormat.Object
oChart.ChartType = Graph.XlChartType.xl3DColumnClustered'释放对象
NAR(oChart)
NAR(oOLEFormat)
NAR(oFont)
NAR(oTextRange)
NAR(oTextFrame)
NAR(oShape)
NAR(oShapes)
NAR(oSlide)
NAR(oSlides)
'建立幻灯片3
'增加一个艺术字并应用阴影效果
oSlides = oPres.Slides
oSlide = oSlides.Add(3, PowerPoint.PpSlideLayout.ppLayoutBlank)
oSlide.FollowMasterBackground = False
oShapes = oSlide.Shapes
oShape = oShapes.AddTextEffect(Office.MsoPresetTextEffect.msoTextEffect27, _
"The End", "Impact", 96, False, False, 230, 200)oShadow = oShape.Shadow
oForeColor = oShadow.ForeColor
oForeColor.SchemeColor = PowerPoint.PpColorSchemeIndex.ppForeground
oShadow.Visible = True
oShadow.OffsetX = 3
oShadow.OffsetY = 3'释放对象.
NAR(oShadow)
NAR(oForeColor)
NAR(oShape)
NAR(oShapes)
NAR(oSlide)
NAR(oSlides)
'修改演示文稿中所有幻灯片的放映切换设置。
Dim SlideIdx(3) As Integer
SlideIdx(0) = 1
SlideIdx(1) = 2
SlideIdx(2) = 3oSlides = oPres.Slides
oRange = oSlides.Range(SlideIdx)
oSlideShowTransition = oRange.SlideShowTransition
oSlideShowTransition.AdvanceOnTime = False
oSlideShowTransition.EntryEffect = PowerPoint.PpEntryEffect.ppEffectBoxOut
'放映幻灯片
RunSlideShow()'释放对象
NAR(oSlideShowTransition)
NAR(oRange)
NAR(oSlides)'不保存改变关闭演示文稿
oPres.Saved = True
oPres.Close()'释放对象
NAR(oPres)
NAR(oPresentations)'移去所有事件句柄
RemoveHandler oApp.SlideShowBegin, AddressOf oApp_SlideShowBegin
RemoveHandler oApp.SlideShowNextSlide, AddressOf oApp_SlideShowNextSlide
RemoveHandler oApp.PresentationClose, AddressOf oApp_PresentationClose'退出PPT
oApp.Quit()
NAR(oApp)GC.Collect()
12、下面这两过程用于Button1_Click事件中,用来启动幻灯片放映
Private Sub RunSlideShow()
Dim oSettings As PowerPoint.SlideShowSettings
Dim oSlideShowWindows As PowerPoint.SlideShowWindowsoSettings = oPres.SlideShowSettings
oSettings.StartingSlide = 1
oSettings.EndingSlide = 3oSettings.Run()
oSlideShowWindows = oApp.SlideShowWindows
On Error Resume Next
Do While oSlideShowWindows.Count >= 1
System.Windows.Forms.Application.DoEvents()
LoopNAR(oSlideShowWindows)
NAR(oSettings)
End Sub'NAR用于释放对象
Private Sub NAR(ByVal o As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
Catch
Finally
o = Nothing
End Try
End Sub13、增加事件句柄:
Private Sub oApp_SlideShowBegin(ByVal Wn As Microsoft.Office.Interop.PowerPoint.SlideShowWindow)
'改变放映窗口的位置和大小
Dim oView As PowerPoint.SlideShowViewWith Wn
.Height = 325
.Width = 400
.Left = 100
.Activate()
End With
oView = Wn.View
oView.Next()NAR(oView)
NAR(Wn)
End SubPrivate Sub oApp_SlideShowNextSlide(ByVal Wn As Microsoft.Office.Interop.PowerPoint.SlideShowWindow)
'改变放映窗口指针的颜色和类型
Dim Showpos As Integer
Dim oView As PowerPoint.SlideShowView
Dim oColorFormat As PowerPoint.ColorFormatoView = Wn.View
Showpos = oView.CurrentShowPosition + 1If Showpos = 3 Then
oColorFormat = oView.PointerColor
oColorFormat.RGB = RGB(255, 0, 0)
oView.PointerType = PowerPoint.PpSlideShowPointerType.ppSlideShowPointerPen
Else
oColorFormat = oView.PointerColor
oColorFormat.RGB = RGB(0, 0, 0)
oView.PointerType = PowerPoint.PpSlideShowPointerType.ppSlideShowPointerArrow
End IfNAR(oColorFormat)
NAR(oView)
NAR(Wn)
End SubPrivate Sub oApp_PresentationClose(ByVal Pres As Microsoft.Office.Interop.PowerPoint.Presentation)
'关闭演示文稿前,把它保存为网页格式。
Pres.SaveAs("C:\TestEvents.htm", PowerPoint.PpSaveAsFileType.ppSaveAsHTML)
NAR(Pres)
End Sub







