file not found comException error

2019-08-25 08:32发布

I am trying to save a jpg file into an excel document.

I am getting the exception System.Runtime.InteropServices.COMException{"The specified file wasn't found"}

now the file does exist and is located at C:\test\101.jpg

here is my code

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

the file does exist so I am unsure why I am getting this error and how to correct it??

this is on a windows 8.1 visual studio 2008 3.5 sp1 platform

Any thoughts are greatly appreciated.

0条回答
登录 后发表回答