Skip to content

Instantly share code, notes, and snippets.

@paxperscientiam
Created November 5, 2015 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paxperscientiam/ca2b67be52ca8ee3d725 to your computer and use it in GitHub Desktop.
Save paxperscientiam/ca2b67be52ca8ee3d725 to your computer and use it in GitHub Desktop.
Quite possibly the only way to export UTF8 formatted data from Excel 2011!
'Refer to execShell() by Robert Knight
' Or, to the StackOverflow post by Robert Knight if it still exists: https://stackoverflow.com/questions/6136798/vba-shell-function-in-office-2011-for-mac
' OMFGBBQ IT WORKS!
rngSomeRange.copy
scriptToRun = "export LC_ALL=en_US.UTF-8 && " & _
"touch " & sOutFolder & "/" & sOutFile & ".tex && " & _
"pbpaste > " & sOutFolder & "/" & sOutFile & ".tex"
execShell scriptToRun, exitCode
' This User Defined Function was written by Robert Knight of StackOverflow
' Designed for Excel 2011, though it might work in the Windoze version too.
' It is shared here for posterity.
Private Declare Function popen Lib "libc.dylib" (ByVal command As String, ByVal mode As String) As Long
Private Declare Function pclose Lib "libc.dylib" (ByVal file As Long) As Long
Private Declare Function fread Lib "libc.dylib" (ByVal outStr As String, ByVal size As Long, ByVal items As Long, ByVal stream As Long) As Long
Private Declare Function feof Lib "libc.dylib" (ByVal file As Long) As Long
Function execShell(command As String, Optional ByRef exitCode As Long) As String
Dim file As Long
file = popen(command, "r")
If file = 0 Then
Exit Function
End If
While feof(file) = 0
Dim chunk As String
Dim read As Long
chunk = Space(50)
read = fread(chunk, 1, Len(chunk) - 1, file)
If read > 0 Then
chunk = Left$(chunk, read)
execShell = execShell & chunk
End If
Wend
exitCode = pclose(file)
End Function
Sub RunTest()
Dim result As String
Dim exitCode As Long
result = execShell("echo Hello World", exitCode)
Debug.Print "Result: """ & result & """"
Debug.Print "Exit Code: " & str(exitCode)
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment