-
2009-10-25
怎样使用VB.net处理PPT2003事件 - [PPT插件设计]
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://pptaddins.blogbus.com/logs/49133399.html
使用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随机文章:
学习写作一个PPT插件(入门级) 2009-10-25揭密PowerPoint动画之旋转篇 2008-12-31解密PowerPoint动画之动画类型篇 2008-12-29解密PowerPoint动画之时间线 2008-12-22Jeff Brenman的博客 2008-11-20







