I'm struggling with getting some code to take a picture and transfer the image to the PC and save the file.
It works up to line Set imfile = item.Transfer 'transfers the picture from the camera and then produces an error stating
"Object Variable or With Block variable not set"
I'm completely lost, it's working great and taking the picture but I'm just not able to then transfer the image
Code:
Option Compare Database
Dim objDeviceInfo As WIA.DeviceInfo
Dim objDevice As WIA.Device
Dim objDeviceManager As WIA.DeviceManager
Dim objItem As WIA.item
Dim objImage As WIA.ImageFile
Dim intCount, intLoop As Integer
Dim objFSO As Scripting.FileSystemObject
Dim strTempPicture As String
Private Sub cmdTakePicture_Click()
On Error GoTo Err_btnTakePicture_click
Dim tempfile As String
Dim MyDevice As WIA.Device
Dim item As WIA.item
Dim imfile As WIA.ImageFile
Dim Commondialog1 As WIA.CommonDialog
Dim MyFilename As String
MyFilename = DLookup("[Pic_Location]", "sysProperties") & Forms![frmMembers_Details]![MemberNumber] & ".jpg"
'put the path and name for the location of your temp file here.
tempfile = (MyFilename)
'the next 4 lines deletes the old temp file if it exists
Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
If FileSystemObject.FileExists(tempfile) Then
Kill (tempfile)
End If
'the next two lines set up the configuration
Set Commondialog1 = New CommonDialog
Set MyDevice = Commondialog1.ShowSelectDevice
Set item = MyDevice.ExecuteCommand(wiaCommandTakePicture) 'instructs the camera to take the picture
Set imfile = item.Transfer 'transfers the picture from the camera
'this line saves the picture to a specified file
imfile.SaveFile (tempfile)
'this sets the picture on the form to show the new picture
Me.imgPicture.Picture = (tempfile)
'MsgBox "Picture taken"
Exit_btnTakePicture_click:
Set MyDevice = Nothing
Set item = Nothing
Exit Sub
Err_btnTakePicture_click:
MsgBox Err.Description, vbOKOnly + vbCritical, "Error Taking Picture"
Resume Exit_btnTakePicture_click
End Sub