0

If I look inside of an MS Infopath XML file, the attachments appear to be some kind of binary code in ASCII stored between tags. I don't have the background to identify, so I'm unsure. This should be easy to produce with VBA I would think.

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. If you run that code, the error should be something about the arguments being of the wrong type. In this case, I'm referencing an Excel file, but it could be a number of things.

The code between the tags is something like: stu2zAQvBfIPwi8BhadFCiKwnIOSXNMA8QFeqWptUWYr5Lr1P77rqg4MQLHimoCzUWURHJmdpaPnVxtjC4eI

I don't know if this helps, but I don't know what to make of it.

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


Sub RunThis()
    bin2var "c:\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
Matthew
  • 1,412
  • 2
  • 20
  • 35
tbaker818
  • 121
  • 1
  • 10

2 Answers2

1

The code between the tags...

... is most likely Base64.
So use a Base64 encoder to encode a binary file to Base64-format and then include that string into the XML file.

Assuming that you use VisualBasic, a quick search showed this solution.

zx485
  • 28,498
  • 28
  • 50
  • 59
  • That helps. I'm using VBA, not VB, so the linked solution won't work. I did find the code I need to accomplish most of it, but I still need something to convert the file read in as a string to Byte like some kind of BinToByte routine. I haven't found one that works. Below is a sample of a PDF file. %PDF-1.7 4 0 obj (Identity) endobj 5 0 obj (Adobe) endobj 8 0 obj << /Filter /FlateDecode /Length 49843 /Type /Stream >> stream xœì½ xUÖ0|ï­¥«zïNw'é,ÝÎÞ ² t’0l ICH€°‹È6†qÄ uFQÇÝQGßѰˆ ÅAGqu\PdÔe”aHºÿsow5Ä÷õßïÿŸçû>‹Ôé[·¶[g?çž*FÙ – tbaker818 Sep 12 '19 at 18:28
  • Maybe [this SO question](https://stackoverflow.com/q/169907/1305969) does help you. It's a VBA solution for Base64 encoding. Another solution [can be found here](https://www.dbwiki.net/wiki/VBA_Tipp:_Base64-Kodierung#L.C3.B6sung) ("Aufruf" means "Calling it"). – zx485 Sep 12 '19 at 20:57
0

Here's the solution I found. I'm using this to convert files to what's needed in this case which is Base64:

Function EncodeBase64ForString(strString As String) As String
Dim arrData() As Byte

  arrData = StrConv(strString, vbFromUnicode)

  Dim objXML As MSXML2.DOMDocument60
  Dim objNode As MSXML2.IXMLDOMElement

  Set objXML = New MSXML2.DOMDocument60
  Set objNode = objXML.createElement("b64")

  objNode.DataType = "bin.base64"
  objNode.nodeTypedValue = arrData
  EncodeBase64ForString = objNode.text

  Set objNode = Nothing
  Set objXML = Nothing
End Function

Public Function GetFileBytes(ByVal strPath As String) As Byte()
Dim lngFileNum As Long
Dim bytRtnVal() As Byte

    lngFileNum = FreeFile

    If LenB(Dir(strPath)) Then ''// Does file exist?
        Open strPath For Binary Access Read As lngFileNum
        ReDim bytRtnVal(LOF(lngFileNum) - 1&) As Byte
        Get lngFileNum, , bytRtnVal
        Close lngFileNum
    Else
        Err.Raise vbObjectError + 1, , "Could not find " & strPath
    End If

    GetFileBytes = bytRtnVal

    Erase bytRtnVal
End Function
tbaker818
  • 121
  • 1
  • 10