Macro to save embended binary file to disk

Hi,

I’m looking into ways to save a binary file to disk with a macro. I’ve played with arrays and streams but the file is really big and it is quite painful to have to use an array of Bytes representing the full file to the save it to disk with streams.
Is there any way to embed the file into the calc document (like an image for example) and then have it saved to disk. For now here’s what I’ve done with an array of bytes and DataOutputStream:

Sub Exploit()
Dim oSFA As Object, oOutText As Object
Dim FileURL As String

oSFA = createUNOService("com.sun.star.ucb.SimpleFileAccess")
FileURL = ConvertToURL("/tmp/truff")

oOutText = createUNOService("com.sun.star.io.DataOutputStream")
oOutText.setOutputStream(oSFA.openFileWrite(FileURL))

oOutText.writeByte(144)

oOutText.flush

End Sub

Thanks in advance !

See attached file. The following macro saves the embed graphic as a file named Logo.png in the same folder as the current document.

Option Explicit

Sub Test
  Dim srcURL As String, destURL As String, oSFA As Object, arr
  oSFA = CreateUnoService("com.sun.star.ucb.SimpleFileAccess")
  
  srcURL="vnd.sun.star.tdoc:/" & ThisComponent.RuntimeUID & "/Pictures/10000001000002200000007899847603.png"
  arr=Split(ThisComponent.URL, "/")
  arr(Ubound(arr))="Logo.png"
  destURL=Join(arr, "/")
  
  oSFA.copy srcURL, destURL
End Sub

Example.ods (43.3 KB)

2 Likes

Hi @sokol92,

this works great and is exactly what I was looking for. Thank you !

truff

1 Like

is that big and it is quite painful ? :thinking:

maybe that’s what you intend to do … SFDocuments.Calc service - ExportRangeToFile

well, that was an example, a real image would take millions of writeByte() :wink: