Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel LoadPicture方法失败_Excel_Vba - Fatal编程技术网

Excel LoadPicture方法失败

Excel LoadPicture方法失败,excel,vba,Excel,Vba,我已成功地将图像加载到Oracle表中。当我尝试在VBA中将同一图像检索到图像控件时,LoadPicture方法失败。这是我使用的代码 Private Sub Image_Retreive() Dim v_RecId As Long Dim v_Str As String With cn .Provider = "OraOLEDB.Oracle" .ConnectionString = "Data Source=" & cboD

我已成功地将图像加载到Oracle表中。当我尝试在VBA中将同一图像检索到图像控件时,LoadPicture方法失败。这是我使用的代码

Private Sub Image_Retreive()

    Dim v_RecId As Long
    Dim v_Str As String

    With cn
        .Provider = "OraOLEDB.Oracle"
        .ConnectionString = "Data Source=" & cboDsn.Value & ";" & _
                            "User ID=" & txtuser.Value & ";" & _
                            "Password=" & txtpwd.Value & ";"
        .Open
    End With

    Rs.Open "select image,file_name from images where file_name=" & "'" & _ 
             fsys.GetFileName(txtFileName.Value) & "'", _
             cn, adOpenKeyset, adLockOptimistic

    Strm.Open
    Strm.Type = adTypeBinary
    Strm.Write (Rs.Fields(0).Value)
    Image1.Visible = True

    Image1.Picture = LoadPicture(Rs.Fields(1).Value)

    Rs.Close
    cn.Close
    Strm.Close
    Set Strm = Nothing
    Set cn = Nothing
    Set Rs = Nothing
已尝试保存到本地文件:

If v_Image_Size > 0 Then 
    'Write the content of the stream object to a file 
    Strm.SaveToFile ("c:\temp\" & Rs.Fields(1).Value), adSaveCreateOverWrite 
    'Load the temp Picture into the Image control 
    Image1.Picture = LoadPicture("c:\temp\" & Rs.Fields(1).Value) 
    'imgPhoto.Picture = LoadPicture(App.Path & "\temp\emp.bmp") 
Else 
    MsgBox "Error reading the Photo" 
End If
出现以下错误:

运行时错误-对象“\u应用程序”的2147418113(8000ffff)方法“LoadPicture”失败

这对我很有用:

Dim fso As New Scripting.FileSystemObject
Dim stm As New ADODB.Stream
Dim tmpFileName

'get a temporary file name
tmpFileName = fso.GetSpecialFolder(TemporaryFolder) & "\" & fso.GetTempName

'open a recordset "rs"

With stm
    .Type = adTypeBinary
    .Open
    .Write rs.Fields(0).Value   'write image bytes to stream
    .SaveToFile tmpFileName, adSaveCreateOverWrite
End With
Sheet1.Image1.Picture = LoadPicture(tmpFileName)

前一阵子同样的代码也起作用了。现在我得到以下错误。运行时错误2220 Microsoft Access无法打开文件“IMG_0125.JPG”您的错误提到Access,但您正在使用Excel/Oracle?您好。我正在使用Oracle。不知道它为什么告诉Access。如果您查看上面的代码,我使用的是provider:provider=“OraOLEDB.Oracle”
Rs.Fields(1)。Value
将是您记录集中的第二个字段,但是您不想要
图像
,它将是
字段(0)
?如果这不是问题,那么考虑将图像写入一个临时文件并从那里加载。记录集以0开始。因此rs.field(0)是图像,rs.fields(1)是文件名。所以代码没有问题。如果可以的话,让我试试你的建议。我使用了你的代码,但仍然得到运行时错误-2147418113(8000ffff)方法“LoadPicture”的对象“\u应用程序”失败如果你找到了临时文件,你能打开它吗?图像的格式是什么?文件类型是TMP文件,我可以使用mspaint打开它。您可以手动将图像控件的Picture属性设置为该文件并正确显示吗?嗨,Tim,即使使用了您的代码,我也已经找出了哪里出了问题。您的代码没有问题。我已经删除了一些不必要的引用(工具/引用),它们会导致一些冲突并产生错误。谢谢你的时间和帮助。