I don't think I explained this situation clearly enough. The command function is in the auto-open macro, as contained in ThisWorkbook section of the VBAProject for Excel workbook I am opening. I have tried opening this workbook using a Powershell script, another workbook using the Shell function, and the command line itself. In all cases, Command() returns a null result, whether the command itself contains a parameter or not. I am expecting the result to at least show "Excel FileName xlsm", or whatever was used to start the Excel process.
Hello
Simon Goodman
Thank you very much for explaining further. After reading your explanation, I understand you have a user-defined function named
Command, which is used in an
Excel VBA Workbook_Open event. You have tried opening the workbook using various methods, including a
PowerShell script, another workbook using the Shell function, and the command line. Still, the
Command() function does not always return the expected result.
To demonstrate the situation, I have created an Excel file named "
Excel FileName.xlsm". Within this Excel file, I have written an
Excel VBA Workbook_Open event and used a user-defined function named
Command.
Excel VBA Code:
Code:
Private Sub Workbook_Open()
Dim userInput As String
userInput = InputBox("Please enter a value:")
MsgBox Command(userInput)
End Sub
Function Command(inputValue As String) As Integer
Command = 2041
End Function
![Command function and Workbook_Open event.png Command function and Workbook_Open event.png](https://exceldemy.com/forum/data/attachments/1/1287-71ce1b1f01103f4e8019a4bff6874dfb.jpg?hash=cc4bHwEQP0)
Later, I successfully opened the Excel file using
Windows PowerShell ISE. Consequently, the
Workbook_Open was triggered, and the Command function was used, returning the intended value.
Code within PowerShell ISE:
Code:
$excel = New-Object -comobject Excel.Application
$FilePath = "C:\Users\User\Downloads\Excel FileName.xlsm"
$workbook = $excel.Workbooks.Open($FilePath)
![Output of running PowerShell code.gif Output of running PowerShell code.gif](https://exceldemy.com/forum/data/attachments/1/1286-91cc653b2ea1ae803955c829aced5f52.jpg?hash=kcxlOy6hro)
I hope you have found the idea helpful.
Regards
Lutfor Rahman Shimanto
ExcelDemy