将功率点(ppt)文件转换为图像

时间:2015-04-16 20:42:37

标签: vb.net powerpoint

我目前正在尝试自动化一种方法,将powerpoint幻灯片转换为我可以在我的应用程序中使用的图像。我使用powerpoint addin的方法:

Private Function convert_slide(ByVal targetfile As String, ByVal imagepath As String, ByVal slide_index As Integer)
    Dim pptapplication As New Microsoft.Office.Interop.PowerPoint.Application
    Dim prsPres As Microsoft.Office.Interop.PowerPoint.Presentation = pptapplication.Presentations.Open(targetFile, True, False, False)
    prsPres.Slides(slide_index).Export(imagepath, "jpg", 0, 0)
    prsPres.Close()
    pptapplication.Quit()


    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(prsPres)
    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(pptapplication)

    Return Image.FromFile(imagepath)

End Function

现在这适用于一个文件,但如果我尝试再次运行该函数,则表示目标路径正在使用中。似乎power point是锁定文件。我不希望每次运行时都更改文件名。我想每次都重用那个临时文件。任何想法如何使文件没有被锁定?

enter image description here

1 个答案:

答案 0 :(得分:0)

这是一个Com对象。你需要处理它。

Private Function convert_slide(ByVal targetfile As String, ByVal imagepath As String, ByVal slide_index As Integer) As Image
  Dim pptapplication As New Microsoft.Office.Interop.PowerPoint.Application
  Dim prsPres As Microsoft.Office.Interop.PowerPoint.Presentation = pptapplication.Presentations.Open(targetFile, True, False, False)
  prsPres.Slides(slide_index).Export(imagepath, "jpg", 0, 0)
  prsPres.Close()
  pptapplication.Quit()
  Microsoft.Office.InteropMarshal.FinalReleaseComObject(prsPres)
  Microsoft.Office.InteropMarshal.FinalReleaseComObject(pptapplication) 
  Return Image.FromFile(imagepath)

End Function