0

so I'm making a small "editor" in PowerPoint using VBA, and I was wondering if there's a way to get the location/file path of a specific open PowerShell window. I want to be able to open PowerShell in a specific folder, using VBA.

BluPY
  • 1
  • 1
  • While, with install-on-demand utilities, it is seemingly possible to get the working directory of a running process - see https://stackoverflow.com/q/20576834/45375 - that won't help with PowerShell, unfortunately, as it maintains its own working directory that is separate from the process' (and can include non-file-system locations). So I don't think what you're trying to do is possible, at least not without running code that is designed to report the location in your PowerShell windows, or perhaps a custom `prompt` function that reflects the location in the _window title_. – mklement0 Oct 29 '21 at 13:23

1 Answers1

0

You can directly run PowerShell Command in VBA :

Cmd = "PowerShell -Command ""{Your Command}"""
Shell(Cmd, vbNormalFocus)

Or you can also execute a script from a folder :

Shell("PowerShell ""Script Path"", vbNormalFocus)

So maybe one of the workaround you can have is executing your shell directly in VBA and specify the path you want in your VBA cmd string.

Also, I think this can help you.

TourEiffel
  • 3,385
  • 2
  • 15
  • 37
  • Thank you for the answer, but I figured I'd rather make it easier for the user. So to run some Python code, I came up with this: ```vb Sub OpenTerminal() Dim filePath As String filePath = Slide1.Label1.Caption Terminal = Shell("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe ""powershell.exe -Command py filePath""", vbNormalFocus) End Sub ``` Although this seems to be fine, it just uses the command "py filePath" instead of say, py C:\Users\Lenovo\Desktop\PyAtom\convert.py. I was wondering if there's any workarounds to this? – BluPY Nov 01 '21 at 09:12
  • @BluPY what is slide1.label1.caption ? Is it a vba useform ? – TourEiffel Nov 01 '21 at 16:26
  • no the caption is the filepath – BluPY Nov 06 '21 at 16:56
  • @BluPY then just do : `Shell("PowerShell " & slide1.label1.caption, vbNormalFocus)` – TourEiffel Nov 18 '21 at 12:04