Aquiring a webcam image using VBA

diddi

Well-known Member
Joined
May 20, 2004
Messages
3,337
Office Version
  1. 2010
Platform
  1. Windows
Greets all,
i am trying to grab an image from a webcam (and ultimately an ultrasound scanner) with the user clicking a single button to aquire the image. i am guessing that i need to find a simple app that can take the snapshot and then control it using WinAPI, but im out of my depth a bit when it comes to API. if anyone has some thoughts or can point me towards a resource id be most grateful.
thx
 
Hi!

It is about an ActiveX Control.

I first tried x360 Video capture ActiveX Control, but this one seems to have no support anymore.
So I tried another one, VideoCapX from fathsoft, which has still active support! =)
But both are not free. So my company is going to pay a license for VideoCapX.

To use it, you have to install the software, and then add the ActiveX control in Excel references.
The trial version allowed me to have a good view, and to develop my own macro quite correctly.
You can get the soft from there Fath Software - Products - VideoCapX - video capture process player ActiveX control or there VideoCapX - Video capture ActiveX control and Video player OCX

A useful help file is included with a couple of coding examples in VBA.
If you need a kick to start in the programing, you can still ask me. But don't forget to take a look at the .chm (or something like that, anyway, the help file) file.

Good luck!
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Thank you for the reply!

I got it to work by calling a separate program via the Shell, to take the picture. The software that actually takes the picture is freeware (CommandCam).
For anyone else struggling with this issue, you can find more info about CommandCam and it's download link at Running CommandCam from Excel using VBA | ad hocumentation.

Working code below, based on the CommandCam example. The "dsm" sub just outputs random information in a cell, it's for debug purposes.
You basically need a button to call "getSnapshot", that's it. Hope it helps someone!

Sub getSnapshot()

Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")

Dim RetVal, base, i, picName, picPath

picNumber = picNumber + 1
picName = "image.bmp"

' Make sure the current directory is set to the one
' where the Excel file is saved
ChDir (ActiveWorkbook.Path)
base = ActiveWorkbook.Path & "\"
picPath = base & picName

'MsgBox (base)
' First, delete image file if present
If objFSO.FileExists(picPath) Then
dsm "Deleting old file..."
Kill (picPath)
End If



' Now, wait until image file is definitely gone
i = 0
While Dir(picPath) > "" Or i > 10
Application.Wait (Now + TimeValue("00:00:01"))
dsm "Waiting to be sure the image is gone... " & i
i = i + 1
Wend
'MsgBox ("about to take a picture")

' Capture new image
RetVal = Shell(base & "CommandCam.exe /filename """ & picPath & """", 1)

' Wait until image file is definitely there
i = 0
While Dir(picPath) = "" Or i > 20
Application.Wait (Now + TimeValue("00:00:01"))
dsm "Waiting for the image to appear... " & i
i = i + 1
Wend

' Short delay to let new file finish saving
dsm "Waiting another two seconds"
Application.Wait (Now + TimeValue("00:00:02"))

' Load new image into image object on spreadsheet
dsm "Attempting to load the picture on a new tab "
NewPictureSheet picPath, "Picture " & picNumber
dsm "all done"
End Sub

Sub dsm(msg As String)
Cells(6, 9).Value = msg
End Sub


Sub NewPictureSheet(ByVal picLocation As String, ByVal sheetName As String)

dsm "Creating a new sheet..."
' Create a new sheet
Dim newsheet
Set newsheet = Sheets.Add(After:=Sheets(Worksheets.Count), Count:=1, Type:=xlWorksheet)
newsheet.Name = sheetName
Worksheets(sheetName).Activate

' Put the picture in the new sheet
'pasteAt = Cells(2, 2)
Cells(1, 1).Select 'This is where picture will be inserted

ActiveSheet.Pictures.Insert (picLocation)

Application.ScreenUpdating = True

Exit Sub

ErrNoPhoto:
MsgBox "Unable to Find Photo" 'Shows message box if picture not found
Exit Sub
End Sub
 
Upvote 0
How do you change to another webcam that is connected to the computer? I trying to get it to take multiple picture with multiple camera's.
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,844
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top