PDA

View Full Version : Convert a file to binary for insertion in an xml file (to work as an attachment)



tbaker818
08-30-2019, 12:18 PM
I've seen things on the web that seem close to what I'm looking for, but I haven't quite made it work.

If I look inside of an MS Infopath XML file, the atttachments appear to be binary code stored between tags. This should be easy to create with VBA, but I don't quite have the background to do it.

The code I'm using is below. In this case I'm trying to get the text from an Excel file that I can embed. When run, the error should be something about the arguments being of the wrong type.


Const adTypeBinary = 1Const adTypeText = 2
Const adModeReadWrite = 3


Sub RunThis()
bin2var "c:\users\n902797\documents\IYYMMCC Validation.xlsx"
End Sub


Function bin2var(filename As String) As String
Dim f As Integer
f = FreeFile()
Open filename For Binary Access Read Lock Write As #f
bin2var = Space(FileLen(filename))
Get #f, , bin2var
thestring = BytesToString(bin2var, CdoUS_ASCII)
Close #f
End Function


Function BytesToString(bytes, charset)
With CreateObject("ADODB.Stream")
.Mode = adModeReadWrite
.Type = adTypeBinary
.Open
.Write bytes
.Position = 0
.Type = adTypeText
.charset = charset
BytesToString = .ReadText
End With
End Function