找不到文件comException错误

时间:2014-03-07 10:42:17

标签: vb.net excel excel-interop

我正在尝试将jpg文件保存到Excel文档中。

我收到异常System.Runtime.InteropServices.COMException {“找不到指定的文件”}

现在该文件确实存在,位于C:\ test \ 101.jpg

这是我的代码

Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles Button1.Click

    Dim xlApp As Excel.Application
    Dim xlWorkBook As Excel.Workbook
    Dim xlWorkSheet As Excel.Worksheet
    Dim misValue As Object = System.Reflection.Missing.Value

    xlApp = New Excel.ApplicationClass
    xlWorkBook = xlApp.Workbooks.Add(misValue)
    xlWorkSheet = xlWorkBook.Sheets("sheet1")

    'add some text
    xlWorkSheet.Cells(1, 1) = "http://vb.net-informations.com"
    xlWorkSheet.Cells(2, 1) = "Adding picture in Excel File"

    'the file c:\test101.jpg does exist?

    xlWorkSheet.Shapes.AddPicture("C:\test\l01.jpg", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 50, 50, 300, 45)

    xlWorkSheet.SaveAs("C:\test\vbexcel.xlsx")


    xlWorkBook.Close()
    xlApp.Quit()

    releaseObject(xlApp)
    releaseObject(xlWorkBook)
    releaseObject(xlWorkSheet)

    MsgBox("Excel file created , you can find the file c:\test\")
End Sub

Private Sub releaseObject(ByVal obj As Object)
    Try
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
        obj = Nothing
    Catch ex As Exception
        obj = Nothing
    Finally
        GC.Collect()
    End Try
End Sub

End Class

该文件确实存在,所以我不确定为什么我收到此错误以及如何纠正它?

这是在Windows 8.1 visual studio 2008 3.5 sp1平台上

非常感谢任何想法。

0 个答案:

没有答案