At the moment, I am building a program in Visual Basic 2010 Professional. When a button is pressed, I need the following to run...
Shell(CMD.exe)
SendKeys.Send("SETX CCDeviceID " & DeviceID & "")
SendKeys.Send()
SendKeys.Send("{ENTER}")
SendKeys.Send("exit")
SendKeys.Send("{ENTER}")
Shell("C:\Python27\python C:\FusionTechnology\stage1.py")
SendKeys.Send("{ENTER}")
SendKeys.Send("exit")
and this works, but the python script starts running before the SETX command completes. Any ideas on how I can wait for the SETX to run and finish before executing the next command?
Thanks!
I would use the System.Diagnostics.Process class, something like this;
Dim vProcess As Diagnostics.Process
vProcess = Process.Start("SETX.exe", "CCDeviceID " & DeviceID)
vProcess.WaitForExit()
vProcess = Process.Start("C:\Python27\python", "C:\FusionTechnology\stage1.py")
Saves messing about with commands in the shell one line at a time!
Related
I am trying to open Windows Explorer and highlight a file.
I am using:
Private Sub cmdLoadStl_Click()
Dim shellCmd As String
shellCmd = "explorer.exe /select, """ & Me.txtPath & """,vbMaximizedFocus"
Debug.Print shellCmd
Shell (shellCmd)
End Sub
This works as expected except the window opens minimized.
I have tried this on several Win7 machines running Access 2016 and two Win10 machines, one running Access 2013, the other running Access 2016.
Is there any way to get the window to open maximized?
Edit: More information - If I paste the command generated by debug.print in my code to a command prompt and run it, the explorer window opens maximized, with the file highlighted, as it should.
The command generated from vba looks like this.
explorer.exe /select, "C:\Users\user\Desktop\filename.txt",vbMaximized
Thanks.
Try like this:
shellCmd = "explorer.exe /select, """ & """,vbMaximizedFocus
shell shellcmd,vbMaximizedFocus
The second (optional) algorithm of shell is the focus. vbMaximizedFocus opens it maximized:
I need to install my win service. With installUtil it is just few lines of code.
#ECHO OFF
REM The following directory is for .NET 2.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727
set PATH=%PATH%;%DOTNETFX2%
echo Installing MyService...
echo ---------------------------------------------------
InstallUtil /i MyService.exe
echo ---------------------------------------------------
echo Done.
pause
But my thoughts are without creating .bat file and then runing it.
Is there any way i can ".execute" those lines of code above without creating .bat file runing it and then deleting it ?.
I will need to dynamically create this code every time because i need to enter the username/password depending what user entered on .net form.
You could start cmd and doing it in one line via it's arguments:
Process.Start("cmd.exe", "/k set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727 & set PATH=%PATH%;%DOTNETFX2% & InstallUtil /i MyService.exe")
And if you want it to show the text you wrote and to "pause" (stay open):
Process.Start("cmd.exe", "/k set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727 & set PATH=%PATH%;%DOTNETFX2% & echo Installing MyService... & echo --------------------------------------------------- & InstallUtil /i MyService.exe & echo --------------------------------------------------- & echo Done. & pause")
Commands are separated by " & ".
I know it's been a month since you first asked this, but I recently came up with a pretty good solution to this - only using this simple VB.NET code:
Public Sub InstallService(ByVal ServicePath As String)
Dim InstallUtilPath As String = IO.Path.Combine(System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory(), "installutil.exe")
Dim InstallUtilProcess As Process = Process.Start(InstallUtilPath, """" & ServicePath & """")
InstallUtilProcess.WaitForExit()
'Service is now installed.
InstallUtilProcess = Process.Start(InstallUtilPath, "/i """ & ServicePath & """")
InstallUtilProcess.WaitForExit()
'The second action is now done. Show a MessageBox or something if you'd like.
End Sub
The ServicePath parameter is the path to the service that you want to install.
The InstallUtilPath variable will be set to the path of the installutil.exe application. It will get the path for the current framework you're running.
As I'm running .NET Framework 4 the path is C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe
Hope this helps!
When I call a shell command in vba, the program opens as does the command prompt but nothing happens and no results are obtained. I can not seem to figure it out and need some help please.
Command that works
C:\Program Files\BioDiscovery\ImaGene 9.0>ImaGene.exe -batch "C:\Users\cmccabe\Desktop\EmArray\Design\test_11_19_2015.bch"
My attempt in excel 2010 using VBA (which opens the program and command prompt, but does not execute the command).
Dim Par As String
Par = "dir c:\Program Files\BioDiscovery\ImaGene 9.0\ImaGene.exe -batch "C:\Users\cmccabe\Desktop\EmArray\Design\imagene.bch"
Call Shell("C:\WINDOWS\system32\cmd.exe /c " & Par, 1)
MsgBox ("ImaGene analysis complete")
May not be the best answer but the below solution works for me for similar situation.
open a notepad "c:\Program Files\BioDiscovery\ImaGene 9.0\ImaGene.exe" -batch "C:\Users\cmccabe\Desktop\EmArray\Design\imagene.bch"
save as say "test.bat"
In excel call this .bat file
Dim wshell As Object
Set wshell = CreateObject("wscript.shell")
wshell.Run Chr(34) & "full path\test.bat"
Hope this helps.
I am developing a tool on Visual Studio 2010 which has a button which executes a powershell program. But before this execution we need to change the path on cmd prompt.
cd Try & powershell C:\Users\Medha\Try\out.ps1
, this statement works fine on cmd prompt but in my VB code, both the commands are taken together and executed at once, which needs to be one by one.
I have tried this
> Shell("cmd.exe /k" + "cd Try & powershell C:\Users\Medha\Try\out.ps1")
Please suggest changes to make it work.
Why don't you use WorkingDirectory property
Dim myProcess As New System.Diagnostics.Process
//if it's in system directory use Environment.SystemDirectory
myProcess.StartInfo.WorkingDirectory = "your\working\directory"
myProcess.StartInfo.FileName = "powershell.exe"
myProcess.StartInfo.UseShellExecute = True
myProcess.Start
Hi can anyone tell me how to execute my .vbs from within VBA cause the following code dose not work.
RetBat4 = Shell("c:\VTS\QUEEN ANNES REVENGE\SYSTEM\VBS\UNDO_2.vbs", 1)
VBA debug says that the sytax is wrong????
thanks
Try
Shell("cscript ""c:\VTS\QUEEN ANNES REVENGE\SYSTEM\VBS\UNDO_2.vbs""",1)
If your VBS needs complete command shell environment, use this:
Shell("cmd /c cscript ""c:\VTS\QUEEN ANNES REVENGE\SYSTEM\VBS\UNDO_2.vbs""",1)
and if your program should wait until the VBS ends, read this post:
VBA Shell and Wait with Exit Code
try This:
Set ws=CreateObject("Wscript.shell")
set ws.exec("ping " & ip & " -n 20")
set ws=nothing