У меня есть код, который открывает powerpoint из моего приложения vb.net, запускает слайд-шоу, а затем закрывает его. Мне было очень больно в ягодицах, чтобы найти все ком-объекты, которые мне пришлось сбросить потом. Вот код:
Imports MSWord = Microsoft.Office.Interop.Word
Imports MSPowerPt = Microsoft.Office.Interop.PowerPoint
Imports MSExcel = Microsoft.Office.Interop.Excel
Dim MSPowerPtApp As MSPowerPt.Application
Dim MSPowerPtPresentation As MSPowerPt.Presentation
Dim MSPowerPtPresentations As MSPowerPt.Presentations
Dim MSPowerPtSettings As MSPowerPt.SlideShowSettings
Dim MSPowerPtSlideShowWindow As MSPowerPt.SlideShowWindow
Dim MSPowerPtSlideShowWindows As MSPowerPt.SlideShowWindows
Function Display_PowerPoint_Show(ByVal filename As String)
Dim MSPowerPtSlides As MSPowerPt.Slides
Display_PowerPoint_Show = True
Try
If (Not FileExists(filename)) Then
Display_PowerPoint_Show = False
MsgBox("Display_PowerPoint_Show: Text file: " & filename & " not found", MsgBoxStyle.Information, "File Not Found")
GoTo Exit_Display_PowerPoint_Show
End If
MSPowerPtApp = New MSPowerPt.Application
MSPowerPtApp.Visible = True
MSPowerPtApp.WindowState = MSPowerPt.PpWindowState.ppWindowMinimized
'Create a new presentation that is based on the specified template.
MSPowerPtPresentations = MSPowerPtApp.Presentations
MSPowerPtPresentation = MSPowerPtPresentations.Open(lbFiles.SelectedValue, True)
MSPowerPtSlides = MSPowerPtPresentation.Slides
If (MSPowerPtSlides.Count = 0) Then
MsgBox("This Powerpoint file has no slides", MsgBoxStyle.Information, "No Slides in File")
GoTo ClosePowerPointFile
End If
MSPowerPtSettings = MSPowerPtPresentation.SlideShowSettings
MSPowerPtSettings.StartingSlide = 1
MSPowerPtSettings.EndingSlide = 1
''Run the slide show and wait for the slide show to end.
MSPowerPtSlideShowWindow = MSPowerPtSettings.Run()
MSPowerPtSlideShowWindows = MSPowerPtApp.SlideShowWindows
Do While MSPowerPtSlideShowWindows.Count >= 1
System.Windows.Forms.Application.DoEvents()
Loop
'Close the presentation without saving changes and then quit MSPowerPt.
MSPowerPtPresentation.Saved = True
MSPowerPtPresentation.Close()
RemoveComObjRef(MSPowerPtSlideShowWindow)
RemoveComObjRef(MSPowerPtSlideShowWindows)
RemoveComObjRef(MSPowerPtSettings)
ClosePowerPointFile:
RemoveComObjRef(MSPowerPtSlides)
RemoveComObjRef(MSPowerPtPresentation)
RemoveComObjRef(MSPowerPtPresentations)
'Quit MSPowerPt.
MSPowerPtApp.Quit()
RemoveComObjRef(MSPowerPtApp)
GC.Collect()
Catch ex As Exception
Display_PowerPoint_Show = False
MsgBox("Display_PowerPoint_Show - File: " & filename & ", Error: " & ex.Message & " reading file", _
MsgBoxStyle.Information, "Error Reading File")
End Try
Exit_Display_PowerPoint_Show:
End Function
'RemoveComObjRef function to remove reference.
Private Sub RemoveComObjRef(ByVal ComObject As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(ComObject)
Catch
Finally
ComObject = Nothing
End Try
End Sub
Надеюсь, это поможет кому-то пропустить дополнительные усилия, которые мне пришлось приложить.