VBA running python - Script does nothing

Asked

Viewed 321 times

0

Good afternoon, I have a VBA command calling a specific Python script. What happens is that if I run the script manually, it works perfectly, no problem at all. Running by VBA it only gives the first Print command and nothing else.

VBA code:

Sub RunPython()
    Dim PythonScript As String
    Dim args As String
    Dim wsh As Object: Set wsh = VBA.CreateObject("WScript.Shell")
    PythonScript = """C:\Users\lgfrota\Desktop\Teste\StaticTest2.py"""
    args = "C:\Python27\Lib\idlelib\idle.py"    
    Shell "C:\Python27\python.exe" & " " & args & " -r " & PythonScript
End Sub

Python code:

lista = []
for file
name in os.listdir('.'):
  if ".dat" in filename:
    lista.append(filename)
    print filename
print lista
for i in range(len(lista)):  
  #Fazer coisas
print(ag,fx,fy,fz,mx,my,mz)
  resultsnam = [ag, fx, fy, fz, mx, my, mz]
  for rnam in resultsnam:
    file.write(rnam.center(19, " ")+"|")
  file.write("\n"*3)

Remembering that the file works perfectly when run separately.


Obs: The Filename print works but run manually it responds all the correct files of the folder. Running by vba, the answer is:

Custom Office Templates
desktop.ini
My Music
My Pictures
My Videos
  • 1

    Your VBA is starting python in your user’s folder C:\Users\lgfrota\Documents, and there is no file .dat

  • Even running the script in a folder that has this . dat? When I played the full path in python, it apparently worked perfectly. I’m trying to solve now, the way to make python get the filepath as argument delivered by VBA

  • The folder where the script is not important; What matters is the folder that the VBA is considering as "current".

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.