1

I am trying to run my Python script from an Excel VBA macro. Originally I did not have an issue because the .py file was in the same directory as the excel file. Now we would like to store the .py file in a different directory and the macro produces an error.

Original Macro:

RunPython ("import pythonefile; pythonfile.get_data()")

I would like to do something like the following, but am not sure how to change the directory that the macro imports the .py file from.

ChDir "C:\"
RunPython ("import pythonfile; pythonfile.get_data()"
MMac11
  • 59
  • 1
  • 1
  • 4
  • why don't you use a complete path? or you can use a browser to select the file if it cannot find the python file in the previously selected directory – Ibo Jul 11 '18 at 21:06
  • You need to set the PYTHONPATH (either in your addin or config file) in order for RunPython to find the file, see: http://docs.xlwings.org/en/stable/addin.html#global-settings – Felix Zumstein Jul 12 '18 at 06:50

1 Answers1

0

Had the same problem. Turns out that you need to change the xlwings default path. In VB Editor go to xlwings.xlam ( if password is required it is "xlwings"), open the modules folder and select module "main". Add in Wsh.currentdirectory = "C:\" in Sub ExecuteWindows as highlighted in the picture below.

enter image description here

WLC
  • 127
  • 1
  • 9