How to execute ipynb file in excel VBA code - python-3.x

I am trying to execute some python scripts through VBA code.
.py files are working pretty well, but i am struggling with .ipynb files
running python script(.py) as following link :
https://stackoverflow.com/questions/73258152/how-to-exceute-ipynb-file-in-excel-vba-code),
but the same way doesn't work for ipynb files.
After quite long searching, i found this article
https://stackoverflow.com/a/62657958/19118788
and in this article, below code is suggested to run ipynb file in excel VBA.
Sub execute_notebook()
Dim objShell As Object
Dim new_cmd_window As String
Dim full_script As String
Dim activate_env As String
Dim change_dir_script As String
Dim convert_and_run_nb As String
Set objShell = VBA.CreateObject("Wscript.Shell")
new_cmd_window = "cmd /c"
activate_env = "cd /d [path to Anaconda\Scripts folder which on my machine is
C:\ProgramData\Anaconda3\Scripts] & activate [environment_name] &"
change_dir_notebook = "cd /d [path to folder where notebook is] &"
convert_and_run_nb = "jupyter nbconvert --to notebook --execute [notebook name].ipynb"
full_script = new_cmd_window & " " & Chr(34) & activate_env & " " & change_dir_script & " " & convert_and_run_nb & Chr(34)
objShell.run full_script
End Sub
Most of lines are understood, but i have no idea for [environment_name] in the above code.
(I am beginner to these things)
Please tell me, what should i do for [environment_name] ??

Related

VBA: Help running cmd line from excel vba

I use a program that extracts data from pdf files and you can use cmd line to control the program, i would like to intergrate this with my excel sheet, the code below works when pasted in to cmd just fine but when i try from VBA it isnt working.
this is what ive managed to find online, help will be very appreciated.
sCommandToRun = "C:\Program Files (x86)\A-PDF Data Extractor\PDECMD.exe" -R"Accord new" -F"C:\Users\phill\Desktop\Test.pdf" -O"C:\Users\phill\Desktop\results.xlsx" -Txlsx -PA
Call Shell("cmd.exe /S /C" & sCommandToRun, vbHide)
aslo if there is a way to wait until the cmd line has finished before i excute another line of code would also be a big help.
Thanks
Add quotes around the paths with spaces in them.
Option Explicit
Sub test()
Dim sCommand As String
sCommand = """C:\Program Files (x86)\A-PDF Data Extractor\PDECMD.exe""" & _
" -R""Accord new""" & _
" -F""C:\Users\phill\Desktop\Test.pdf""" & _
" -O""C:\Users\phill\Desktop\results.xlsx"" -Txlsx -PA"
'Debug.Print sCommand
Shell "cmd.exe /S /C " & sCommand, vbHide
End Sub
changing '"Shell "cmd.exe /C " & sCommand, vbHide" to "Shell sCommand, vbHide" did the trick some some reason – phill cook just now Edit

Record in a .txt file the generated results of an .exe

I have and Excel file which contains the data for a Python code. Python code is called from a .exe, and I can see in the PowerShell the results which that code is generating.
Sub Button()
Dim strPName As String
Dim strData As String
strPName = Range("H24").Text
strData = "--inputs"
Call Shell("""" & strPName & """ """ & strData & """", vbNormalFocus)
End Sub
When I have issues in the code or with the data input, the PowerShell is switch off and I cannot review which was the issue.
I want to include in the button code an automatic .txt file generation, which records the PowerShell text and let me review it after the stop.
I found here How to save the ouput of shell command into a text file in VBA the following command:
Shell "ftp -n -i -g -s:" & vPath & "abc.txt " & vFTPServ & " >> test.txt", vbNormalFocus
But I don't know how to connect it properly in my current button.
Could you give me some suggestions of how could I solve it?

VBA Shell command with variables and spaces

In a Microsoft Outlook macro, I'm trying to use a Shell() call to open an Excel spreadsheet (whose filepath is referenced by my templatePath variable). I keep getting syntax errors from the editor and "file not found" errors when executing it.
I started with the following line:
Shell ("""C:\Program Files (x86)\Microsoft Office\Office16\EXCEL.EXE"" ""C:\Users\My_Username\Desktop\My_Folder\Request Template.xlsx"""), vbNormalFocus
It opens the appropriate file just fine; I just don't know the proper syntax to use the templatePath variable instead of hard-coding the path to the spreadsheet. I've seen questions similar to this, but none seemed to fit my situation closely enough. Any help would be greatly appreciated!
This should work:
Dim templatePath As String
templatePath = "C:\Users\My_Username\Desktop\My_Folder\RequestTemplate.xlsx"
Shell ("""C:\Program Files (x86)\Microsoft Office\Office16\EXCEL.EXE"" """ & templatePath & """"), vbNormalFocus
If your template resides in the Application.TemplatesPath and you just want to specify the filename then use:
templatePath = Application.TemplatesPath & "RequestTemplate.xlsx"
A more adjustable version:
Dim templatePath As String
Dim programPath As String
Dim templateName As String
templateName = "RequestTemplate.xlsx"
templatePath = Application.TemplatesPath
programPath = "C:\Program Files (x86)\Microsoft Office\Office16\EXCEL.EXE"
Shell ("""" & programPath & """" & " " & """" & templatePath & templateName & """"), vbNormalFocus

Running Autoit script from Excel VBA

Im trying to run autoitscript from excel vba using a button click.
I wanted to run notepad1.au3 script from an excel sheet.
i copied the script into the same directory as excel sheet.
I wrote the following vba code to run the script.Everything seems to work fine, it takes the path file name etc accurately.
But instead of just running the script, an explorer window pops up ,asking me to locate the script i want to run.
I can browse to the location of the script through the explorer window and select the script file and it will run.
But i want it to run without opening an explorer window.
Any idea where could be the problem?
Thanks
VBA Code:
Sub Autoit()
Dim AutoItPath
Dim FileName As String
Dim FileName1 As String
FileName = ThisWorkbook.Path & "\notepad1.au3"
MsgBox (FileName)
AutoItPath = "C:\Program Files (x86)\AutoIt3" & "\AutoIt3.exe "
MsgBox (AutoItPath)
FileName1 = """" & AutoItPath & """" & """" & FileName & """"
MsgBox (FileName1)
runscript = Shell(FileName1)
End Sub
Your call is wrong. See the help file:
Run a script using the interpreter:
AutoIt3.exe [/ErrorStdOut] [/AutoIt3ExecuteScript] filename [params ...]
Execute the AutoIt3 script 'filename' with optional parameters
example command: "'" & "C:\Program Files (x86)\AutoIt3\AutoIt3.exe " /AutoIt3ExecuteScript "..path\notepad1.au3" & "'"

Calling BAT with VBA - CMD Loads and Closes Immediately

I'm using VBA to write a command file and a BAT file which calls PLink to log in to a server and run a script. The Plink portion is now functioning as expected thanks to some help on here. Unfortunately VBA seems to be having some difficulty with opening the cmd and finding plink as a usable command.
I've tried multiple different ways of calling it, but the results are all the same. The cmd prompt opens and then closes immediately giving me an error stating:
"plink is not recognized as an internal or external command, operable program, or batch file."
The files generate perfectly, and if I run the bat file outside of VBA, it runs as expected. With the files, I keep a copy of plink.exe on the desktop. I've even tried adding a copy of cmd.exe to the desktop with the links and plink.exe as well, but I get the same problem.
Here are some of the ways I've tried, though modified for my code: 1 2 3
Here's the most recent code for the macro:
Dim vPath As String
Dim vscript As String
UserID = Range("F1").Value
Passwd = Range("F2").Value
Server = Range("F3").Value
StartDate = Range("F4").Value
EndDate = Range("F5").Value
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile "C:\Program Files (x86)\PuTTY\plink.exe", "C:\Users\MikeDesktop\"
'fso.CopyFile "C:\Windows\System32\cmd.exe", "C:\Users\Mike\Desktop\"
vPath = "C:\Users\Mike\Desktop"
Open vPath & "\pcmd.txt " For Output As #1
Print #1, "cd /opt/test/srt"
Print #1, "./srt.tool " & StartDate & " " & EndDate & " > /opt/test/srt/test.txt &"
Print #1, "rm test.txt"
Print #1, "sleep 60; exit"
Close #1
Open vPath & "\Chg.bat" For Output As #1
Print #1, "plink " & UserID & "#" & Server & " -pw " & Passwd & " < " & vPath & "\pcmd.txt"
Close #1
vscript = vPath & "\Chg.bat"
Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 5
wsh.Run vscript, windowStyle, waitOnReturn
Application.Wait (Now + TimeValue("0:01:00"))
Kill vPath & "\Chg.bat"
Kill vPath & "\pcmd.txt"
Kill vPath & "\plink.exe"
If someone can tell me why the cmd isn't recognizing plink as an option, that would be much appreciated
Either pass the path to plink.exe in the batch file, or make sure that %Path% includes the folder that contains plink.exe.

Resources