2015-04-16 43 views
1

我目前正在嘗試將PowerPoint幻燈片轉換爲可以在我的應用程序中使用的圖像的自動化方法。我倒有使用PowerPoint播放插件的方法:將功率點(ppt)文件轉換爲圖像

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 

現在這個工程的一個文件,但如果我嘗試運行函數再次它說,目標路徑正在使用中。看起來,電源點正在鎖定文件。我不想在每次運行時更改文件名。我想每次重用該臨時文件。任何想法如何使文件不被鎖定?

enter image description here

回答

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 
+0

我加了條紋線,但仍然是同樣的問題。我現在更新了我的問題以反映這一點。 –

+0

應用程序變量是否具有Exit/Quit方法?如果是這樣,在處置之前調用它。 – OneFineDay

+0

我叫pptapplication.Quit()但仍然是相同的鎖定文件錯誤。 –