Function does'nt work !!

MadameHugo

New Member
Joined
Feb 25, 2020
Messages
9
Office Version
  1. 2010
Platform
  1. Windows
Hi,

After many researches, I used this function to convert my images path into format BASE64.

VBA Code:
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

But when i call this function like : =EncodeFile(G2)

NOTE : G2 that contain the PATH OF IMAGE : C:\Users\dalila\Pictures\images\naom_59dddfc7dd0b0.jpg

I have the result "#VALUE!"

Can you help me please? ...
 

Attachments

  • this.png
    this.png
    21.6 KB · Views: 21

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Call the function from VBA and it will work for you
VBA Code:
Sub CallFunc()
    MsgBox EncodeFile("C:\Users\dalila\Pictures\images\naom_59dddfc7dd0b0.jpg")
    MsgBox EncodeFile(Range("G2"))
End Sub
 
Upvote 0
Hi, it work's but i need to do that for all of my range in cells not in messages boxs ( because i have many images...)

Thank you again Yongle..
 
Upvote 0
Use VBA to loop
- something like this
VBA Code:
Dim cel As Range
For each cel in Range("G2:G20")
   cel.Offset( ,1) =EncodeFile(cel)
Next cel
 
Upvote 0
Always the same...

The thing that i don't understand is that when a call the function, i have "#VALUE!". Please see my file and just tell me where is my error :

 
Upvote 0
Good morning Yongle,

After many tries, i found that if I download an image from the web, it will be converted in BASE64. But the images who are situated in my PC can't be converted. I don't understand that.

May be if I use your funtion :

Dim cel As Range
For each cel in Range("G2:G20")
cel.Offset( ,1) =EncodeFile(cel)
Next cel

This will be the solution. But this function have error, this is :
call_function.png
 
Upvote 0
Try this
VBA Code:
Dim cel As Range
For each cel in Range("G2:G20")
   cel.Offset( ,1) =EncodeFile(cel.Value)
Next cel
 
Upvote 0

Forum statistics

Threads
1,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

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