i would like to use the API of https://ocr.space/ to look for given text positions. I saw some topics but none of them was VBA related.
My code at the moment is:
Sub test()
Dim pic As String
Dim httpReq As New XMLHTTP60
Dim UserName, Key As String
UserName = Sheets("main").Range("B8")
Key = Sheets("main").Range("B9")
pic = "C:\Users\myname\Desktop\Capture.png"
pic = EncodeFile(pic)
strURL = "https://api.ocr.space/Parse/Image"
httpReq.Open "POST", strURL, False, UserName, Key
httpReq.setRequestHeader "Content-Type", "form-data" ' "application/x-www-form-urlencoded?"
httpReq.setRequestHeader UserName, Key
httpReq.send "base64Image=data:image/png;base64," & pic & "&isOverlayRequired=true"
resp = httpReq.responseText
MsgBox resp
End Sub
The code I use to get the base64 string of the picture is the following, however it seems to work fine, as I can get back the picture if i paste it to an online converter.
Public Function EncodeFile(strPicPath As String) As String
Const adTypeBinary = 1 ' Binary file is encoded
' Variables for encoding
Dim objXML
Dim objDocElem
' Variable for reading binary picture
Dim objStream
' Open data stream from picture
Set objStream = CreateObject("ADODB.Stream")
objStream.Type = adTypeBinary
objStream.Open
objStream.LoadFromFile (strPicPath)
' Create XML Document object and root node
' that will contain the data
Set objXML = CreateObject("MSXml2.DOMDocument")
Set objDocElem = objXML.createElement("Base64Data")
objDocElem.DataType = "bin.base64"
' Set binary value
objDocElem.nodeTypedValue = objStream.Read()
' Get base64 value
EncodeFile = objDocElem.Text
' Clean all
Set objXML = Nothing
Set objDocElem = Nothing
Set objStream = Nothing
End Function
Could you please help me to find what do I do wrong?