I have a variable in my VBA script and I'm trying to call that variable on the command line within the script.
Sub Test()
'Set enviro to %APPDATA%
Dim enviro As String
enviro = CStr(Environ("APPDATA"))
'Create a new variable that sets the file path for the RepoDir.txt
RepoPath = enviro & "\RepoDir.txt"
'Create a new variable to grab the line of text in RepoDir.txt
Dim FilePath As String
Dim strFirstLine As String
'The new variable calls the RepoPath Variable
FilePath = RepoPath
Open FilePath For Input As #1
Line Input #1, strFirstLine
'MsgBox (strFirstLine)
Shell ("cmd /c cd call strFirstLine & git pull RandomSig > %TEMP%\gitPull.txt 2>&1")
End Sub
I am trying to call the variable strFirstLine which contains a a pathway I want the command line to read and then CD to it. Is there anyway to do this?
Thank you!
You will want to pass the value of strFirsLine, not the name. Use (example):
Shell ("cmd /c cd " & strFirstLine & "git ..."
Related
I have a question on how I should approach this code that I need. I have been searching for the past 3 hours and couldnt find a way to mesh the codes together to perform what I desired. Here is the outline of what I would like to do:
The User runs the macro and the InputBox pops up which tells them to Input a Folder Name. From there, I want the Application.GetOpenFile (or which ever application or dir function) to initiate a search in a specified directory for a folder and NOT a file (as there may be several files in the folder with similar names), but take the user to the folder and then the User selects the right file to open. Then once the file is selected, it is opened in the excel worksheet.
Try using shell. Here's a function from user bburns.km on this question Capture output value from a shell command in VBA?
This combined with a shell command to only list folders and directories should get you where you need.
Public Function ShellRun(sCmd As String) As String
'Run a shell command, returning the output as a string'
Dim oShell As Object
Set oShell = CreateObject("WScript.Shell")
'run command'
Dim oExec As Object
Dim oOutput As Object
Set oExec = oShell.Exec(sCmd)
Set oOutput = oExec.StdOut
'handle the results as they are written to and read from the StdOut object'
Dim s As String
Dim sLine As String
While Not oOutput.AtEndOfStream
sLine = oOutput.ReadLine
If sLine <> "" Then s = s & sLine & vbCrLf
Wend
ShellRun = s
End Function
Public Sub test()
Dim path As String
path1 = "C:\File Folder\File Test.pdf"
path2 = "C:\FileFolder\FileTest.pdf"
Shell "C:\Program Files (x86)\Nuance\PDF Professional 8\bin\GaaihoDoc.exe " & path1, vbNormalFocus
End Sub
The above code in VBA works if the path2 variable is used instead of path1, because of the spaces in the path1 variable. How can it be fixed to run the variable path1?
Paths with spaces need to be quoted with double-quotes.
Escape the quotes by doubling them up:
path1 = "C:\File Folder\File Test.pdf"
Shell """C:\Program Files (x86)\Nuance\PDF Professional 8\bin\GaaihoDoc.exe"" """ & path1 & """", vbNormalFocus
I need to create a function in Excel that allows me to read a value form one cell that contains the name of an Active Directory Group and obtain the members of that group and load that information in another cell.
I use something like this to run my Ruby scripts from Excel.
Create a button and edit the macro behind it.
Replace the command and script executed with the one you need for your AD search.
The example takes the value of cell A2 and uses it as input in the script to produce the output in cell D2.
Sub Knop1_Klikken()
Dim objShell As Object
Dim objWshScriptExec As Object
Dim objStdOut As Object
Dim rline As String
Dim strline As String
Dim arg As String
Dim command As String
arg = Worksheets("Blad1").Range("A2")
Set objShell = CreateObject("WScript.Shell")
command = "cmd.exe /S /C ruby ""C:\Users\Gebruiker\ruby\excel\run.rb"" " & arg
Set objWshScriptExec = objShell.Exec(command)
Set objStdOut = objWshScriptExec.StdOut
While Not objStdOut.AtEndOfStream
rline = objStdOut.ReadLine
If rline <> "" Then strline = strline & vbCrLf & rline
Wend
Worksheets("Blad1").Range("D2") = strline
End Sub
I'm trying to pass a variable from a VBS to BAT but i'm getting "The system cannot find the file specified"
Here is my vbs :
Option Explicit
Dim strFile
strFile = SelectFile( )
If strFile = "" Then
WScript.Echo "No file selected."
Else
WScript.Echo """" & strFile & """"
End If
Function SelectFile( )
Dim objExec, strMSHTA, wshShell
SelectFile = ""
strMSHTA = "mshta.exe ""about:" & "<" & "input type=file id=FILE>" _
& "<" & "script>FILE.click();new ActiveXObject('Scripting.FileSystemObject')" _
& ".GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);" & "<" & "/script>"""
Set wshShell = CreateObject( "WScript.Shell" )
Set objExec = wshShell.Exec( strMSHTA )
SelectFile = objExec.StdOut.ReadLine( )
Dim wshShelll
Set WshShelll = Wscript.CreateObject("WScript.Shell")
WshShelll.Run "C:\Users\nbendjelida\Desktop\email.bat" & SelectFile
Set objExec = Nothing
Set wshShell = Nothing
Set wshShelll = Nothing
End Function
here is my bat :
"C:\Program Files\Microsoft Office\Office12\Outlook.exe" /eml %1
do you have any idea ?
I repeat the right answer of sachadee with more details to get this question removed from the list of unanswered questions.
Run Method must be called with first parameter being the command to execute with the parameters exactly as when entering the command in the command line window. The examples on the referenced Microsoft help page have also a space character after the command Notepad.
The command line required to call the batch file with a file name as first parameter is:
C:\Users\nbendjelida\Desktop\email.bat name_of_selected_file
But the Windows script host code line
WshShelll.Run "C:\Users\nbendjelida\Desktop\email.bat" & SelectFile
builds the string for the command to run as
C:\Users\nbendjelida\Desktop\email.bat name_of_selected_file
because of the missing space character.
The problem is solved with the correct Windows script host code line
WshShelll.Run "C:\Users\nbendjelida\Desktop\email.bat " & SelectFile
because of the space charater between name of batch file and name of selected file.
If name of selected file contains 1 or more spaces, it is necessary that either variable SelectFile contains already a double quote at beginning and at end, or the necessary double quotes are added on concatenating the command string.
Example with entire batch file name also containing a space character:
Dim FileName
FileName = "%TEMP%\Any File.txt"
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run """%USERPROFILE%\Desktop\My Batch File.bat"" """ & FileName & """"
The batch file My Batch File.bat on desktop of current user containing
#echo %0 %*
#pause
outputs for example on Windows 7
"C:\Users\username\Desktop\My Batch File.bat" "C:\User\username\AppData\Local\Temp\Any File.txt"
or on English Windows XP
"C:\Documents and Settings\user name\Desktop\My Batch File.bat" "C:\Documents and Settings\user name\Local Settings\Temp\Any File.txt"
which are the expected results for the command string.
(Yes, a user name can contain a space character although Microsoft recommends not to use a space character in user name, see Microsoft page Creating User and Group Accounts.)
I need this script to look for a folder in
c:\windows\system32\foldername.
If this folder exists then i want the script to stop. If the folder does not exist then i want the script to run a .exe file from the server toinstall a piece of software. ie
\servername\folder\software.exe.
There will be file sin the folder in c:\windows\system32\foldername but i only wnat it to look at the folder not its contents.
Any suggestions please.?
you could try the following script
Option Explicit
Private Const Folder As String = "c:\windows\system32\foldername"
Private Const FileToRun As String = "\\servername\folder\software.exe"
Sub Run(ByVal sFile)
Dim shell
Set shell = CreateObject("WScript.Shell")
shell.Run Chr(34) & sFile & Chr(34), 1, False
Set shell = Nothing
End Sub
Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
If Not fs.FolderExists(Folder) Then
Run FileToRun
End If