0

Using VBA, I want to open QGIS (the actual GUI) and execute a simple script through the Python console or the editor.

7
  • would shell help? msdn.microsoft.com/en-us/library/xe736fyk(v=vs.90).aspx I seem to recall that this was a VBA function. Jun 6 '14 at 2:09
  • but shell would just start QGIS, it wouldn't go further i want to run a script inside the python console or editor after starting QGIS
    – amamdouh
    Jun 6 '14 at 2:13
  • That's true. Sometimes programs take command line arguments which would allow you to give QGIS instructions... Shell is a single command so when I used it I wrote to a batch file and shelled the batch file and deleted it when it was done. As for getting inside QGIS with VBA then no, you can't do that. Jun 6 '14 at 2:16
  • Can't it be done with Python, create a .py that can open QGIS and start a script in the Python console/editor?
    – amamdouh
    Jun 6 '14 at 2:24
  • I think so, that's just a very slight modification of the write to batch and then shell. I've also done that before but in SML for TntMips. Jun 6 '14 at 2:27
6

QGIS can take a --code that you can pass a Python file to in order to run.

qgis --code my myfile.py

Run this command from the VBA shell command.

5
  • I used Shell ("C:\Program Files (x86)\QGIS Valmiera\bin\qgis.bat --core my test.py"), but when QGIS starts it gives an error "Layer is not valid, the layer C:\Users\Home\Documents\--core is not a valid layer and can not be added to the map", can you please explain how to use this command.
    – amamdouh
    Jun 6 '14 at 3:24
  • I tried --code not --core and it worked, thanks a lot
    – amamdouh
    Jun 6 '14 at 3:44
  • 1
    Beware of spaces in path names, it looks like it opens in "c:\users\home\documents" by default. If you want to use a file called "my test.py" it should be quoted or it becomes two arguments at the command line, best to call it "my_test.py" Jun 6 '14 at 5:10
  • It works fine, thank you. How can i can run another .py in the same QGIS instance, if I use the Shell command again, it opens another instance.
    – amamdouh
    Jun 7 '14 at 17:09
  • You don't have a lot of options. The only option that would allow communication like that is maybe some kind of COM object using Python and connect to it in VBA. Python can expose a class as a COM object.
    – Nathan W
    Jun 8 '14 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.