I am trying to run a Python Script through Excel VBA
I have tried the following code, but nothing happened:
Here is the VBA code:
RetVal = Shell("C:\Program Files\Python37\python.exe C:\Users\kailung\Desktop\kai\VBA\june_Longchen\test\VBA.py")
My python script is just renaming files within a folder (I've tested it in Python IDLE, it is fine):
import os, sys
dir_path = os.path.dirname(os.path.realpath(__file__))
# Open a file
dirs = os.listdir(dir_path)
i=1
# This would print all the files and directories
for file in dirs:
if file!='VBA.xlsm' and file!='VBA.py' and file!='~$VBA.xlsm':
print(file) #print all the files
i=str(i) #change to string for file name
i=i + '.xls' #add extension
os.rename(file,i) #rename the file
size=len(i)
i=i[0:size-4] #remove the extension
i=int(i) #change back to numerical
i=i+1 #new numerical name