+ Reply to Thread
Results 1 to 3 of 3

Create a Base64 String for a PDF file

  1. #1
    Registered User
    Join Date
    07-20-2016
    Location
    UK
    MS-Off Ver
    2010
    Posts
    11

    Question Create a Base64 String for a PDF file

    I need to create a Base64 String which relates to a PDF file that I have. I have tried the below, but the output (EncodeFileBase64) that is generated isn't working - I think the output being produced is being truncated, because when I put the output into a decoder tool, it knows it's a PDF but produces an error when I try to open it.

    Attached are 2 documents, 1 showing what the below code is currently producing when being input into an excel cell, and the other document is showing what the Base64 String should look like (which is a lot longer!)

    Public Sub File_for_Converting()

    FileName = "C:\Geographic Regional Sales Analysis.pdf"
    Call Main.EncodeFileBase64(FileName)

    End Sub
    Function EncodeFileBase64(FileName As String) As String

    Dim arrData() As Byte
    Dim fileNum As Integer

    fileNum = FreeFile
    Open FileName For Binary As fileNum
    ReDim arrData(LOF(fileNum) - 1)
    Get fileNum, , arrData
    Close fileNum

    Dim objXML As MSXML2.DOMDocument
    Dim objNode As MSXML2.IXMLDOMElement

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

    objNode.DataType = "bin.base64"
    objNode.nodeTypedValue = arrData
    EncodeFileBase64 = objNode.Text

    Range("B8").Value = EncodeFileBase64

    Set objNode = Nothing
    Set objXML = Nothing

    End Function
    Last edited by seaside_escape; 04-25-2018 at 09:27 AM. Reason: Solved

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Create a Base64 String for a PDF file

    Write the output to a text file, I doubt that a cell has enough characters

  3. #3
    Registered User
    Join Date
    07-20-2016
    Location
    UK
    MS-Off Ver
    2010
    Posts
    11

    Re: Create a Base64 String for a PDF file

    Of course! Thank you Kyle123, I have now added in the following - and the notepad contains the full Base64 string required. So simple!

    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Dim oFile As Object
    Set oFile = fso.CreateTextFile("C:\Base64Output.txt")
    oFile.WriteLine EncodeFileBase64
    oFile.Close
    Set fso = Nothing
    Set oFile = Nothing

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Encode/Decode a byte array to/from Base64 format
    By jeff_123 in forum Excel General
    Replies: 1
    Last Post: 05-31-2014, 04:54 AM
  2. Convert Byte array to Base64 Binary
    By ganesh961991 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-15-2014, 07:47 AM
  3. Base64 decoding using excel function
    By deepak1986 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-20-2012, 02:21 PM
  4. Replies: 3
    Last Post: 09-30-2011, 04:35 AM
  5. base64 encoding??? in a hyperlink more less..
    By Mrpimpdave in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-11-2009, 02:07 PM
  6. Replies: 1
    Last Post: 08-04-2009, 06:42 AM
  7. Use String Variables in File Path and create path if not existing
    By JanBang in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-26-2007, 09:04 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1