Executing a bat. file with arguments from vba code - vba

I'm trying to execute a bat. file from vba but can't get it working (after viewing some of the threads from the subject).
here is the code now:
Dim filet As String
Dim numero As String
Const pekka = "TIEDOSTO"
Const sami = "NUMEROT"
filet = Range(pekka).Cells(1, 1).value
numero = Range(sami).Cells(1, 1).value
commandstring = "D:"
commandstring2 = "cd folder name"
commandstring3 = "Create-tri.bat" + " " + filet + " " + numero
Call Shell("cmd.exe /S" & commandstring & commandstring2 & commandstring3, vbNormalFocus)
So according to my logic, this code should first access D, then the wanted folder and then execute the bat. file with the given parameters (filet and numero). What am I doing wrong here?
Best regards and thanks in advance,
Johannes

Try to remove the Call, it is depreciated in VBA.
Then use Shell "cmd.exe /S" & commandstring & commandstring2 & commandstring3.
To see what you are actually trying to execute, use:
Debug.Print "cmd.exe /S" & commandstring & commandstring2 & commandstring3
What do you get? Most probably it is not an executable command, ending on .bat Some reference here - Execute .bat file from Excel VBA Macro

Related

Open a file in a new instance of program

All;
I have a bit of code I've written that opens a design blueprint when I scan a bar code. It works well enough, but I'd like to open a new instance of the design software (Solidworks) and have the print display in the new instance. Right now, no matter how many Solidworks instances I have open, the print will only open in the first instance started.
The line commented out below is the line that works, just not in the right instance. The line below that is what I'd expect to work, but it returns a 'file not found' even though the path to solidworks and the print path are both correct.
Any explanation as to why this isn't working would be much appreciated as I'm obviously very new at this...and have no idea what I'm doing.
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim barcode As String = tb_barcode.Text
Dim filename As String = tb_barcode.Text
'Add File Extension to end of path
Dim ext As String = ".SLDDRW"
'Split job number from detail number in barcode textbox
barcode = Split(tb_barcode.Text, ".")(0)
filename = Split(tb_barcode.Text, ".")(1)
'- This works, just in primary instance
'System.Diagnostics.Process.Start("G:\Fixtures\" & barcode & "\Details\" & barcode & " DET " & filename & ext)
'- This does not work
System.Diagnostics.Process.Start("'C:\Program files\Solidworks Corp\Solidwork\SLDWORKS.exe' 'G:\Fixtures\" & barcode & "\Details\" & barcode & " DET " & filename & ext + "'")
Catch
MessageBox.Show("File Not Found")
End Try
End Sub
Sorry for naive approach but shouldn't there be a comma in Process.Start between 2 arguments?
Start(String, String)
Starts a process resource by specifying the name of an application and a set of command-line arguments, and associates the resource with a new Process component. docs
Why don't you use the Application.ExecutablePath.That returns the Application's path with its full name. Then your code should be
System.Diagnostics.Process.Start(Application.Executablepath, "G:\Fixtures\" & barcode & "\Details\" & barcode & " DET " & filename & ext + "'")
Also make sure that the second string argument is a valid path.

How to execute Batch File while passing parameters from Excel

I am trying run a batch file placed at a particular path. The file requires user inputs for which I want the parameters to be passed from Excel cells. This execution of the batch file within Excel should happen by usage of click command button.
I am new to VBA. I tried the following code, but on clicking the button nothing is happening.
Private Sub CommandButton2_Click()
sid = Excel.Worksheets("Sheet1").Range("I8").Value
user = Excel.Worksheets("Sheet1").Range("I9").Value
Password = Excel.Worksheets("Sheet1").Range("I10").Value
msg = "hi"
Shell ("CMD.EXE /c C:\Users\shashank.b03\Desktop\test_CMD.bat" & sid &" "& user &" "& password &" ")
End Sub
Here is an example which I have tested and should work fine for you. It just calls the shell command and passes it a command string.
You can change the path where your batch file is in the string & if you don't want to show the shell window when you're running this use vbHide instead of vbNormalFocus.
You'll just have to change this a bit to put the cell values into the sid, user and password variables.
Hope this helps.
Dim sid As String
Dim user As String
Dim password As String
CommandString = "c:\test.bat" + " " + sid + " " + user + " " + password
Call Shell("cmd.exe /c" & CommandString, vbNormalFocus)
Here is a more basic example of using parameters and a batch file from shell.
Save the following as test.bat
set arg1=%1
echo HELLO %1!
pause
Put this code inside a button or some other component in excel;
Private Sub CommandButton1_Click()
Dim sid As String
sid = "Shashank"
CommandString = "c:\test.bat" + " " + sid
Call Shell("cmd.exe /c" & CommandString, vbNormalFocus)
End Sub
Make sure that the path where the batch file is saved is the same as the one in commandstring.
When this is run, you'll see the string held in the variable sid is passed to the batch file and used. You should be able to get it working from here.
Hope this helps

Vba to call a powershell script or .bat file

I have a line to call a powershell script and it works but the powershell windows gets closed without it working.
the code is, any ideas?
Additionally I would like it to be able to run .bat files (I tried Shell ( & pathcrnt & "\GUI-getuserpropertiesV2.10.ps1" & pathcrnt) bit I get the same error.
Sub CallBatch()
Dim pathcrnt As String
pathcrnt = ActiveWorkbook.Path
Shell ("PowerShell " & pathcrnt & "\GUI-getuserpropertiesV2.10.ps1" & pathcrnt)
End Sub
Please see the link below and make sure you have permissions for the machine.
http://www.tek-tips.com/viewthread.cfm?qid=1585510
I don't know about your setup or security, so if you encounter an error, search for 'PS permissions', or something along those lines. The VBA script will definitely work!
If you insert a space after .ps1 it should work:
Sub CallBatch()
Dim pathcrnt As String
pathcrnt = ActiveWorkbook.Path
Shell "PowerShell " & pathcrnt & "\GUI-getuserpropertiesV2.10.ps1 " & pathcrnt
End Sub
Imagine the resulting string; for pathcrnt C:\Windows it would be this: "PowerShell C:\Windows\GUI-getuserpropertiesV2.10.ps1C:\Windows"
You also might want to enclose the path elements with double quotes to prevent spaces messing with your parameters, like this:
Shell "PowerShell """ & pathcrnt & "\GUI-getuserpropertiesV2.10.ps1"" """ & pathcrnt & """"
Since double quotes also are string delimiters in VBA you have to escape them with another double quote.

File doesn't zip using 7zip command in vb.net

I am trying to zip a file in vb.net. I am using 7zip to do this. I am using the Process.Start method.
Here is the zip line of my code:
Process.Start("C:\Program Files\7-Zip\7z.exe", "a -tzip" + (ChosenFile & "\" & "SavedFiles") + NewFileName1)
No error that I know of happens, however when I look through the path, I cannot find the zipped files.
ChosenFile & "\" & "SavedFiles" is the destination folder.
NewFileName1 is the file to be zipped
you must do a space after "-tzip" and u forgot use " to folders with spaces like that:
Process.Start("C:\Program Files\7-Zip\7z.exe", "a -tzip " & (ControlChars.Quote & ChosenFile & "\" & "SavedFiles" & ControlChars.Quote) & " " & ControlChars.Quote & NewFileName1 & ControlChars.Quote &)
Probably its obvious, but you have the rights to access those files with the user who runs the script?
Else, can you run the script manually step by step to debug where it fails?

downloading file using SFTP with VBA

My objective is to download, not upload a file from an SFTP server, and I am trying to adapt the code from another question on this site to do so (I pasted the code below for your convenience).
I downloaded PSFTP from Putty. PSFTP closes when I try to connect using the following command line:
open username:password#server.com.port:1111
I have three questions:
Is something wrong with my command line? If not then what could be the problem?
As far as I know SFTP would normally utilize get/put commands, but i don't see a put command in the code below, so I don't understand where I should enter the get command to download the file instead of uploading it (which is what the code below is supposed to be doing).
Is it correct that pRemotePath is the location of the file on the SFTP server, and pFile is the location I want the file downloaded to?
A simple explanation would be very much appreciated.
Public Sub SftpGet()
Const cstrSftp As String = """C:\Users\Ron\UtilityTools\psftp.exe"""
Dim strCommand As String
Dim pUser As String
Dim pPass As String
Dim pHost As String
Dim pFile As String
Dim pRemotePath As String
pUser = "uid"
pPass = "PW"
pHost = "dns"
pFile = "C:\Users\Ron\activity.txt"
pRemotePath = "Z:/activity.log"
strCommand = cstrSftp & " -sftp -l " & pUser & " -pw " & pPass & _
" " & pFile & " " & pHost & ":" & pRemotePath
Debug.Print strCommand
Shell strCommand, 1 ' vbNormalFocus '
End Sub
I think you should start with a Windows command prompt session. Work out the details of your command line there, as I suggested in an answer to a similar question: SFTP upload with VBA. Once you have a command line which works there, it will be very easy to execute that same command from VBA.
I've never used Putty's psftp.exe tool, only pscp.exe, so I can't offer help about how to construct your psftp.exe command line. One thing I noticed in Putty's documentation is that PSFTP (pscp.exe) can only work with a SSH-2 server --- if your target server supports only SSH-1, PSFTP will not work.
I think it would be worthwhile for you to review the Putty documentation at that link.