|
| 1 | +Attribute VB_Name = "PyWimVBA5" |
| 2 | +' Pywim VBA Function (5.3) |
| 3 | +' A module to run python via VBA, By DatCanFly |
| 4 | +' Copyright by DatCanFly (2023) |
| 5 | + |
| 6 | +Function GenerateRandomString(length As Integer) As String |
| 7 | + Dim randomString As String |
| 8 | + Dim charSet As String |
| 9 | + Dim i As Integer |
| 10 | + |
| 11 | + charSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" |
| 12 | + |
| 13 | + For i = 1 To length |
| 14 | + randomString = randomString & Mid$(charSet, Int((Len(charSet) * Rnd) + 1), 1) |
| 15 | + Next i |
| 16 | + |
| 17 | + GenerateRandomString = randomString |
| 18 | +End Function |
| 19 | +Private Function WriteToFile(filePath As String, content As String) As Boolean |
| 20 | + Dim fileNumber As Integer |
| 21 | + Dim delimiter As String |
| 22 | + ' Open the file for writing |
| 23 | + On Error GoTo ErrorHandler |
| 24 | + fileNumber = FreeFile |
| 25 | + Open filePath For Output As fileNumber |
| 26 | + delimiter = ";;" |
| 27 | + rel_cont = Split(content, delimiter) |
| 28 | + ' Write content to the file |
| 29 | + For Each item In rel_cont |
| 30 | + Print #fileNumber, item |
| 31 | + Next item |
| 32 | + |
| 33 | + |
| 34 | + ' Close the file |
| 35 | + Close fileNumber |
| 36 | + |
| 37 | + ' Return success |
| 38 | + WriteToFile = True |
| 39 | + Exit Function |
| 40 | + |
| 41 | +ErrorHandler: |
| 42 | + ' An error occurred, return failure |
| 43 | + WriteToFile = False |
| 44 | +End Function |
| 45 | +Public Function RunPy(code As String, pythonPath As String, Optional keepFileData As Boolean = False, Optional UseDebug As Boolean = False) As String |
| 46 | + Dim command As String |
| 47 | + Dim cmd As String |
| 48 | + Dim fileContent As String |
| 49 | + Dim wshShell |
| 50 | + Dim filename As String |
| 51 | + Dim outputFilePath As String |
| 52 | + filename = GenerateRandomString(5) & ".txt" |
| 53 | + outputFilePath = GenerateRandomString(6) & ".py" |
| 54 | + |
| 55 | + If WriteToFile(filename, code) Then |
| 56 | + ' Nothing there |
| 57 | + Else |
| 58 | + MsgBox "[PyW.VBA] Error occurred while writing to the file." |
| 59 | + End If |
| 60 | + If UseDebug <> False Then |
| 61 | + command = "cmd /K " & " """ & """" & pythonPath & """" & " """ & filename & """ > """ & outputFilePath & """" & """ " |
| 62 | + Else |
| 63 | + command = "cmd /K " & " """ & """" & pythonPath & """" & " """ & filename & """ > """ & outputFilePath & """" & """ " & " & exit" |
| 64 | + End If |
| 65 | + 'MsgBox command |
| 66 | + |
| 67 | + Set wshShell = CreateObject("WScript.Shell") |
| 68 | + |
| 69 | + If UseDebug <> False Then |
| 70 | + wshShell.run command, vbNormalFocus, True |
| 71 | + ' # Show cmd - Debug |
| 72 | + Else |
| 73 | + wshShell.run command, vbHide, True |
| 74 | + End If |
| 75 | + ' # Do not Show cmd - Stable |
| 76 | + 'exitCode = wshShell.Run(command, vbHide, True) |
| 77 | + ' Execute the command using the Shell function |
| 78 | + 'Shell command, vbHide |
| 79 | + If Dir(outputFilePath) <> "" Then |
| 80 | + Open outputFilePath For Input As #1 |
| 81 | + fileContent = Input$(LOF(1), 1) |
| 82 | + Close #1 |
| 83 | + If keepFileData <> True Then |
| 84 | + Kill outputFilePath |
| 85 | + End If |
| 86 | + Else |
| 87 | + fileContent = False |
| 88 | + |
| 89 | + End If |
| 90 | + If keepFileData <> True Then |
| 91 | + Kill filename |
| 92 | + End If |
| 93 | + RunPy = fileContent |
| 94 | +End Function |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
0 commit comments