0

I would like to take a picture using a webcam from excel macros, and I am a complete beginner when it comes to excel. I tried to follow other sources on the internet, but I can't even type in "Public Class Form1" without getting an error. What I did was I inserted a button into a spreadsheet, clicked "New," then replaced the sub data that comes with the button with code I found on the internet, and I just have tons of red lines throughout the code.

I looked at this post and its answers, but none of it works because, as I've said, excel doesn't want to work. If it's possible for you guys to give me more baby-step instructions (use this button and not that button, you gotta type "Public Class Form1" then pray to the gods for it to work instead of just pressing enter, stuff like that), that would help me greatly.

Visual Basic Connect to Webcam and Save Picture to File

Me not knowing how to terminate line. I tried &HD and &HA since that's a solution from microsoft, but that obviously didn't work. Me not knowing how to terminate line

CruleD
  • 1,153
  • 2
  • 7
  • 15
Albert Garcia
  • 23
  • 1
  • 8
  • That question is VB.Net, not VBA. – BigBen Jun 02 '20 at 21:05
  • Sorry. I edited the tag. – Albert Garcia Jun 02 '20 at 21:08
  • My point was that VB.Net and VBA are not the same thing. You can't use VB.Net in an Excel macro, which is VBA. – BigBen Jun 02 '20 at 21:10
  • Thank you. I didn't know there was a difference between the two. Helped me a bit, but I got tired of trying to get excel to do the whole process, so I made the executive decision of downloading Python on my boss's computer and use super simple code that I already knew that worked. It'll help him out since he's been wanting to learn Python, so win-win-win – Albert Garcia Jun 02 '20 at 22:47

1 Answers1

0

The answer is don't try to do the whole thing from excel. In excel, assign a macro to a button. The code in that macro will be:

Sub RunPython()

Dim objShell As Object
Dim PythonExe, PythonScript As String

    Set objShell = VBA.CreateObject("Wscript.Shell")

    PythonExe = """PATH TO PYTHON.EXE"""
    PythonScript = "PATH TO PYTHON CODE THAT NEEDS TO BE RUN"

    objShell.Run PythonExe & PythonScript

End Sub

In Python 3, you, use the following code:

from cv2 import *

cam = VideoCapture(0)   # 0 -> index of camera
s, img = cam.read()
if s:    # frame captured without any errors
    namedWindow("cam-test", WINDOW_NORMAL)
    imshow("cam-test",img)
    waitKey(1000)
    destroyWindow("cam-test")
    imwrite("filename.jpg",img) #save image

Credit:

Using Excel to use Python: https://stackoverflow.com/a/60658227/9582521

Using Python to use Webcam: https://stackoverflow.com/a/11094891/9582521

Albert Garcia
  • 23
  • 1
  • 8