I have a code that enables me to execute the Windows script file (PowerShell script file) and the code works fine with no problems.
What I am trying to do is to pass a variable to a line in the script file. Example:
This line in the script file
convert Image.png -resize 300x300 -density 300 -quality 100 ImageNew.png
The line has the Image.png
but this file name I would like it to be variable and passes a value to it through Excel cell.
In other words I have the file name in a cell and I would like to change Image.png
to be the value in the cell.
This is the script file:
cd (Join-Path $env:USERPROFILE 'Desktop\Output\')
convert Image.png -resize 300x300 -density 300 -quality 100 ImageNew.png
And this is the VBA code I am using:
Sub TestMe()
MsgBox ScriptFile
End Sub
Function ScriptFile() As String
Dim wshShell As Object
Dim strCommand As String
strCommand = "Powershell.exe -File ""C:\Users\Future\Desktop\Sample.ps1"""
Set wshShell = CreateObject("WScript.Shell")
wshShell.Run strCommand, 0, True
End Function
Any idea how to do that?
I tried so far In VBA Code I used this line
strCommand = "Powershell.exe -File ""& {'C:\Users\Future\Desktop\Sample.ps1' 001.png}"""
and in the script file I used this line at the top
$image=$args[0]
But I couldn't make it work yet