I came across this fantastic code on DevHut by Daniel Pineault. I was doing file hashing and found Daniel’s script here:
Get a File Hash using VBA | DEVelopers HUT (devhut.net)
This in turn uses the PS_GetOutput script he explains here:
VBA – Run PowerShell Command | DEVelopers HUT (devhut.net)
And here is the script as of 4/12/2024 on Daniel’s site:
This uses the power of the command line to execute a PowerShell command and places the (valid) output onto the clipboard and then retrieves the latest clipboard entry and returns that text from the function output.
This is fantastic and works beautifully.
However, I did run into a problem with the function when there is no output returned from the command. In this instance, the clip program doesn’t paste anything to the clipboard. This resulted in me getting either an error if the clipboard is empty, or the last string that I had copied (or the output of the last time I ran a successful PS_GetOutput command.
So in order to mitigate the problem of not receiving any output and the resulting undesirable effects, I added a couple of lines to check for this condition. Here is the updated function:
I added the first six lines to setup a constant string and paste it into the clipboard.
Then I check to see in the very last line if the function is equal to the known value. If so we know that there was no output captured to the clipboard. This will prevent others from experiencing a strange and difficult to diagnose issue with some commands.
Thanks Daniel, for your contributions!
Trackbacks/Pingbacks