I am trying to create PowerShell Script which allows me to open excel file, run macro, then save file and close it. My code is below:
$excel = New-Object -ComObject Excel.Application
$MacroName1 = "Extraction_Tool.xlsm"
$currentExecutingPath = $fullPathIncFileName.Replace($currentScriptName, "") + $MacroName
$ExcelFiles = Get-ChildItem -Path $currentExecutingPath
$Workbook = $excel.Workbooks.Open($currentExecutingPath)
$excel.Application.DisplayAlerts = $False
$excel.Run("refreshall")
$excel.ActiveWorkbook.Save()
$workbook.Close()
$excel.Quit()
When I am running this script I receive error:
You cannot call a method on a null-valued expression.
At W:\GDA_files\SME_iQA\iQA_NEW\PrepareMacro.ps1:10 char:1
+ $excel.ActiveWorkbook.Save()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
VBA macro (refreshall) takes about one minute. Do you think that script cannot save file because it is trying to do this while macro is running?
$excel.ActiveWorkbookis empty. Try$workbook.Save()instead. - Ansgar Wiechers$workbook.Save() $workbook.Close()I receive:Method invocation failed because [System.__ComObject] doesn't contain a method named 'Save'. At W:\GDA_files\SME_iQA\iQA_NEW\PrepareMacro.ps1:10 char:1 + $workbook.Save() + ~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (Save:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound- Klinik