1

Thanks for looking at my problem. I have a python script which is writing a new csv file in my current directory. I have make it python.exe using pyinstaller. What I am doing is, running this exe file using VBA form excel on a button click. Problem is after running exe using VBA my csv is saving in default documents folder instead of folder where my python.exe is. But If I run the python.exe directly without VBA then it's saving csv in my current directory. So how can I save csv in current directory after running python.exe using VBA

Here is my python code which creating new csv file:

csvfile = open("Mycsv" + '.csv', 'w')
cr = csv.writer(csvfile, dialect='excel')

I have also tried using: csvfile = open(os.getcwd() + "\Mycsv" + '.csv', 'w') But didn't worked.

Here is my VBA code:

    Dim wsh As Object
    Set wsh = VBA.CreateObject("WScript.Shell")
    Dim waitOnReturn As Boolean: waitOnReturn = True
    Dim windowStyle As Integer: windowStyle = 1
    Dim errorCode As Integer
    wsh.Run Chr(34) & ThisWorkbook.path & "\python.exe" & Chr(34), windowStyle, waitOnReturn
2
  • I suspect you need to pass the working directory path from VBA to the python.exe as an argument. The python program has no way to know otherwise which directory your excel file was. It is an independent process. Commented Jul 4, 2018 at 12:44
  • 1
    you can try to change the working directory of WScript with wsh.CurrentDirectory = ... before the wsh.Run ... Commented Jul 4, 2018 at 12:54

2 Answers 2

0

Before saving the file you can use os.chdir(some_path_to_directory) to set the current working directory, and then save in this directory.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you guys for all your help. None other than @Vincent's Answer worked perfectly. So I have used wsh.CurrentDirectory = Thisworkbook.path before wsh.Run... Thanks a lot...!
0

try specifying the path for your csv file:

import os.path
save_path = 'C:/example/'
csvName = os.path.join(save_path, "Mycsv"+".csv") 
csvfile = open(csvName, "w")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.