Shell Command Gives Error "error after : and." - vb.net

I used :
Shell("cmd /k type " & System.IO.Path.GetTempPath & "file.exe > " & Application.ExecutablePath & ":file.exe")
Temp Folder : C:\Documents and Settings\Admin\Local Settings\Temp\
Command Prompt Window gives error : File not found, error after : and. System can't find destination path.
I think the problem is in this name : Documents and Settings
What should I do to bring it to life?
P.S : File exists, it works when I use : start command.

You need extra wrapping quotes.
Shell("cmd /k type """ & System.IO.Path.GetTempPath & "file.exe"" > " & Application.ExecutablePath & ":file.exe")
Spaces are delimiters in command line parameters. If you have a space in a single parameter, you need to wrap the entire parameter in quotes.

you probably need to put your path between "". something like
Shell("cmd /k type \"" & System.IO.Path.GetTempPath & "file.exe\" > " & Application.ExecutablePath & ":file.exe")

Related

Add printer to system using Powershell within VBA?

I can make it work with a batch file using:
powershell.exe "Add-printer -ConnectionName \\PS02.samba.net\HELPS006"
But if I try to run the bat file above with VBA, it does not add the printer:
Shell ("c:\Fraktsedlar\HELPS006.bat")
If I run powershell directly from VBA, none of these works either:
Shell ("powershell.exe -Command " & Chr(34) & "Add-printer -ConnectionName \\PS02.samba.net\HELPS006" & Chr(34))
Shell ("powershell.exe -Command {" & Chr(34) & "Add-printer -ConnectionName \\PS02.samba.net\HELPS006" & Chr(34) & "}")
Shell ("powershell.exe -Command " & Chr(34) & "{Add-printer -ConnectionName \\PS02.samba.net\HELPS006}" & Chr(34))
What is it I'm doing wrong?
If powershell can do it so VBA.
From Help
http://download.microsoft.com/download/winscript56/Install/5.6/W982KMeXP/EN-US/scrdoc56en.exe This contains some libraries for system admin. You can use all of Windows Scripting Host features except for the root wscript object (used by a script to communicate with the host). 1000 times as much admin/info can be used with WMI https://msdn.microsoft.com/en-us/library/windows/desktop/aa393262(v=vs.85).aspx.
WSH has a second add printer function. Look at that one.
Adds a Windows-based printer connection to your computer system.
object.AddWindowsPrinterConnection strPrinterPath
object
WshNetwork object.
strPrinterPath
String value indicating the path to the printer connection.
Example
The following code uses the AddWindowsPrinterConnection method to connect a network printer to a Windows NT/2000 computer system.
Set WshNetwork = CreateObject("WScript.Network")
PrinterPath = "\\printserv\DefaultPrinter"
WshNetwork.AddWindowsPrinterConnection PrinterPath
Try this:
Shell ("powershell.exe -Command { Add-Printer -ConnectionName \\PS02.samba.net\HELPS006 }")
Or this:
Shell ("powershell.exe -Command " & Chr(34) & "& { Add-Printer -ConnectionName \\PS02.samba.net\HELPS006 }" & Chr(34))
You can also try this which is what Add-Printer does:
$p = Get-WmiObject -List Win32_Printer -EnableAllPrivileges
$p.AddPrinterConnection ("\\PS02.samba.net\HELPS006")

Executing a bat. file with arguments from vba code

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

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?

New line in VB.NET

Why, when I do im my code:
"Land Location \\r\\n Roundoff (C)"
I see the \\r\\n and not a new line feeder at the output?
Any idea how to do that?
As I said I must have only one string there, without using a "&". Can I put that vbCrLf inside of my string somehow?
There is no \ escape codes in VB so you can't put a line break in a string literal. The only escape character in VB strings is the double quotation marks used to insert a quotation mark in a string.
You can use the VB constant for a Windows type line break:
"Land Location " & vbCrLf & " Roundoff (C)"
For the code to be platform independent, you should use the NewLine property instead:
"Land Location " & Environment.NewLine & " Roundoff (C)"
Whether you should use the platform independent code or not depends on the situation.
If you need it as a single string for some reason, you would have to use a marker for the line break, that you replace when you use the string:
Dim s As String = "Land Location \n Roundoff (C)"
s = Replace(s, "\n", Environment.NewLine)
May be "Land Location " & vbCR & vbLF .....
--
Edit: per #JeffSahol's comments, you can use string interpolation since VB 14, so it can be like
$"...{vbCrLf}..."
Instead of including the newline manually in the String use System.Environment.NewLine.
vbCrLf
vbCr
vbLf