Skip to content

Commit 90658f3

Browse files
authored
Add files via upload
1 parent c5ca857 commit 90658f3

File tree

3 files changed

+448
-0
lines changed

3 files changed

+448
-0
lines changed

bin/PyWimVBA5.2a.bas

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+

bin/PyWimVBA5.2ag.bas

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

0 commit comments

Comments
 (0)