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
Related
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] ??
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?
I have three String varibales in my VBA code as below:
Dim FilePath As String
Dim FooterFilePath As String
Dim PlusChar As String
FilePath = Application.ActiveWorkbook.Path & "\Bin\file.txt"
FooterFilePath = Application.ActiveWorkbook.Path & "\Bin\footer.txt"
PlusChar = Chr(43) 'i.e. "+" sign
Now I am trying to merge FilePath and FooterFilePath and write the combined result in FilePath again i.e. overwriting it. For that I am using followng line which doesn't work:
Shell "cmd.exe copy /c & FilePath & PlusChar & FooterFilePath & FilePath", 0
I know I have syntaxing issue here but I just don't know how to pass three variables into Shell command.
You have to put the variables outside the enclosing double-quotes. also you should insert blanks between the different names so that your command does not get the names combined in one name. Try this:
Shell "cmd.exe copy /c " & FilePath & " " & PlusChar & " " & FooterFilePath & " " & FilePath, 0
But:
There's no reason to have a variable for the +
Your drive's name is missing in the paths
/C is an option for the cmd program, not for the copy command.
It is useful to print that expression in the immediate window to verify it before execution. To do so, you can put it in a String variable and debug.print it before execution:
This would be correct:
Dim FilePath As String, FooterFilePath As String, myCommand As String
FilePath = Application.ActiveWorkbook.Path & "C:\Bin\file.txt"
FooterFilePath = Application.ActiveWorkbook.Path & "C:\Bin\footer.txt"
myCommand = "cmd /c copy " & FilePath & " + " & FooterFilePath & " " & FilePath
Debug.Print myCommand
Shell myCommand, 0
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" & "'"
I'm trying to use file dialogue to pick a file and save it to a folder but I can't figure out how to code a folder path and keep getting "Path not found error"
I'm not at all a coder, so please explain in plain English:)
Below is the piece of code I tried to use but didn't work.
ArchiveFolderPath = Environ("UserDomain") & "\" & Environ("username") & "\Desktop"
Full sub code from comment below:
Sub CreateCopyFile(FilePathToCopy As String)
Dim fso As Scripting.FileSystemObject
Dim FileToCopy As Scripting.File
Dim ArchiveFolderPath As String
'Create a new folder path
Set fso = New Scripting.FileSystemObject
ArchiveFolderPath = Environ("UserProfile") & "\Desktop\Archive"
'Create a new folder
If Not fso.FolderExists(ArchiveFolderPath) Then
fso.CreateFolder ArchiveFolderPath
End If
Set FileToCopy = fso.GetFile(FilePathToCopy)
FileToCopy.Copy ArchiveFolderPath & "\" & FileToCopy.name
Set fso = Nothing
End Sub
Try adding another backslash at the end of the word, "Desktop", and add two backslashes at the very beginning of the path. Like so:
ArchiveFolderPath = "\\" & Environ("UserDomain") & "\" & Environ("username") & "\Desktop\"
You need to put another \ at the end of Desktop and then make sure that you put the file name after it when you are saving.
So will look like ArchiveFolderPath = Environ("UserDomain") & "\" & Environ("username") & "\Desktop\"
Some of what you are looking for is going to depend on the operating system. In Windows 7, there is a Public Desktop that populates all user desktops and has fairly open file permissions. It is located at:
c:\Users\Public\Desktop
If that is where you want to put things then your code would be one of these,
ArchiveFolderPath = "c:\Users\Public\Desktop"
ArchiveFolderPath = Environ("PUBLIC") & "\Desktop"
If you wanted to point to the current users desktop then that would be one of these,
ArchiveFolderPath = "c:\Users\" & Environ("USERNAME") & "\Desktop"
ArchiveFolderPath = Environ("USERPROFILE") & "\Desktop"
The environment variable that you are using (`Environ("USERDOMAIN")) typically points to the computer name in a workgroup environment although it would be different in a structured Active Directory network.
Remember to use environment variables whenever possible to avoid variations in hard-coded paths between system operating versions. An XP machine's user profiles are not in the same place as a Windows 7 or Windows 8 machine.
A quick and easy way to get a list of the environment variables in the current session is to open a command prompt window and type SET then hit Enter.