I'm trying to execute a PowerShell script that opens an Excel file and does a SaveAs with it. the problem is that it works when I put the full path for the file, but I want it to be directed to the working folder, that way if I take the script and files to other location I don't have to modify the script for each environment.
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$excel.DisplayAlerts = $false
$wb = $excel.Workbooks.Open("E:\TEMP\TEMPLATE4WEEKS.xlsx")#Opening like this works
$wb = $excel.Workbooks.Open(ThisWorkbook.Path & "\TEMPLATE4WEEKS.xlsx") #this is not working
$wb.SaveAs(ThisWorkbook.Path & "\TEMPLATE4WEEKSprotected.xlsx",[Type]::Missing,"password") #this is not working
$excel.Quit()
I was trying to concatenate current folder so it will work if I say move the entire folder to another path.
Right now the folder that contains the PS1 script is the same that contains the TEMPLATE4WEEKS.xlsx file. I want all to operate inside the same folder.
Changed as per Ansgar's response:
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$excel.DisplayAlerts = $false
$file = Join-Path $excel.ThisWorkbook.Path "TEMPLATE4WEEKS.xlsx"
$wb = $excel.Workbooks.Open($file)
$wb.SaveAs($file,[Type]::Missing,"password")
$excel.Quit()
I'm getting this error now:
Join-Path : Cannot bind argument to parameter 'Path' because it is null. At C:\Users\AGUIRRG2\Desktop\Newfolder\PasswordProtect4WEEKSCopy.ps1:4 char:19 + $file = Join-Path $excel.ThisWorkbook.Path "TEMPLATE4WEEKS.xlsx" + ~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Join-Path], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.JoinPathCommand Sorry, we couldn't find . Is it possible it was moved, renamed or deleted? At C:\Users\AGUIRRG2\Desktop\Newfolder\PasswordProtect4WEEKSCopy.ps1:5 char:1 + $wb = $excel.Workbooks.Open($file) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], COMException + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException You cannot call a method on a null-valued expression. At C:\Users\AGUIRRG2\Desktop\Newfolder\PasswordProtect4WEEKSCopy.ps1:6 char:1 + $wb.SaveAs($file,[Type]::Missing,"password") + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
&
in PowerShell is not a concatenation operator. Plus, you don't want to build paths via string concatenation in the first place. You also cannot use the object ThisWorkbook
directly in PowerShell (that only works in VBA, and it's the location of the current macro, so it won't work here at all because you're not running a macro).
Change this:
$wb = $excel.Workbooks.Open(ThisWorkbook.Path & "\TEMPLATE4WEEKSxlsx")
into this:
$wb = $excel.Workbooks.Open("${PSScriptRoot}\TEMPLATE4WEEKS.xlsx")
Note: The automatic variable $PSScriptRoot
was introduced with PowerShell v3 and doesn't exist in earlier versions. If you're running an older version you need to define it yourself, e.g. like this:
$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With