Keeping the excel opened before it completely refreshed through powershell? - excel

I am using this powershell script to open an excel, run a macro to refresh all, save and close.
This is working fine but the excel is saving and closing before refreshing the excel completely.
Is there a way i can add a wait statement so that it can wait until the workbook is completely refreshed.
$objectExcel = new-object -c excel.application
$objectExcel.displayAlerts = $false # don't prompt the user
$objectExcel.visible = $True;
#$objectExcel.visible = $True;
$Classeur = $objectExcel.workbooks.open("\\gbrmiteufs01.eu.xerox.net\SWG01\XOG-BORG\EBIP\POS Data\POS
Data.xlsm", $null, $false)
$objectExcel.run("Workbook_RefreshAll")
$objectExcel.run("RemoveODBC") # another custom macro for removing data connecion
$Classeur.save()
$Classeur.close()
$objectExcel.quit()

Related

PowerShell how to open multiple instances of Excel

My script is opened an Excel sheet from SPO and after the reading from the file and then I closing it.
The issue is when I run this script from 2 instances of PS (Ctrl + T)
While the first script is running all good but when the second script running I get a pop up message that the file is already opened
This is the script that read from the excel
Connect-PnPOnline -Url $SharePointSiteURL
$ExcelObject = New-Object -ComObject Excel.Application
$ExcelWorkBook = $ExcelObject.Workbooks.Open($SharePointSiteURL)
$ExcelWorkSheet = $ExcelWorkBook.Sheets.Item("VIP List")
QuitExcel
function QuitExcel {
# when done, quit Excel and remove the used COM objects from memory (important)
$ExcelWorkBook.Close()
$ExcelObject.Quit()
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ExcelWorkSheet)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ExcelWorkBook)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ExcelObject)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
Disconnect-PnPOnline
While First script is accessing xls, definitely the second script which is accessing the same xls would cause an issue because it is in use. Alternate is to make a copy of the same xls on runtime and use it.

Powershell Run Excel Macros

hello im trying to open an excel document then open an excel Macro Document, then have powershell run the specific macro that i want and let the macro do its magic and call it a day.
the script i have is this:
# start Excel
$excel = New-Object -comobject Excel.Application
#open file
$FilePath = 'C:\Users\Username\Desktop\ExcelWorkbook.xlsm'
$workbook = $excel.Workbooks.Open($FilePath)
#make it visible (just to check what is happening)
$excel.Visible = $true
#access the Application object and run a macro
$app = $excel.Application
$app.Run("Macro")
$excel.Quit()
#Popup box to show completion - you would remove this if using task scheduler
$wshell = New-Object -ComObject Wscript.Shell $wshell.Popup("Operation Completed",0,"Done",0x1)
So my issue is im getting the error "all macros may be disabled"
what code do i use to make them enabled, i'm having issues with that.
$app it's not defined, so try to replace $app.Run("Macro") with $Excel.Run("Macro")

Workbook.SaveAs method is popping Save As dialog box

This was working fine until this weekend, now it's stopped..
I have a powershell script that opens an Excel file as read only, copies a sheet from that file into a new file, then saves the new file to a different location. The script still does all of that, gets to the very end, saves the file to the new location, but now it is opening a "Save as" dialog box, which prevents the script from ever completing. Something change with Powershell? Or the SaveAs method?
# Specify file names/paths
$sourceFile = "C:\source.xlsx"
$exportFile = "C:\export.xlsx"
# Open Excel, hidden/suppress alerts
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $false
$Excel.DisplayAlerts = $false
# Open the source workbook in read-only mode
$sourceWorkbook = $Excel.Workbooks.Open($sourceFile,$null,$true)
# Create a new/blank workbook
$newWorkbook = $Excel.Workbooks.Add()
# Copy sheet from source workbook to new workbook
$sourceWorkbook.Sheets("SheetName").Copy($newWorkbook.Sheets[1])
# Save the new workbook
$newWorkbook.SaveAs($exportFile)

RefreshAll() Inconsistent

Using PowerShell, I want to open several Excel workbooks, refresh all the links inside and then calculate the sheets so that when other users open them, they see the updated links and do not need to refresh the links. My script works, but not for all the sheets.
I discovered that having the sheet set to manual Calculate prevented it from Calculating on Save and Close, so I added script to change the Calculations to Automatic and then back to Manual, but it didn't work for one of the sheets. The only difference between that sheet and the others is that is it in a different folder, but the script is able to access the folder.
$Excel = new-object -comobject excel.application
$Excel.displayAlerts = $false # don't prompt the user
$Excel.visible = $False;
$wb = $Excel.workbooks.open("C:\Folder1\File1.xlsm")
$xlAutomatic = -4105
$xlCalculationManual = -4135
$Excel.Calculation = $xlAutomatic
$wb.RefreshAll() #refresh links
$wb.close($true) #close and save
$wb = $Excel.workbooks.open("C:\Folder1\File2.xlsm")
$wb.RefreshAll() #refresh links
$wb.Calculate
$wb.close($true) #close and save
$wb = $Excel.workbooks.open("C:\Folder1\File3.xlsm")
$wb.RefreshAll() #refresh links
$wb.Calculate
$wb.close($true) #close and save
$wb = $Excel.workbooks.open("C:\Folder1\Folder2\File4.xlsm") # <-----File that is not updating
$wb.RefreshAll() #refresh links
$wb.Calculate
$wb.close($true) #close and save
$wb = $Excel.workbooks.open("C:\Folder1\File1.xlsm")
$wb.RefreshAll() #refresh links
$wb.Calculate
$Excel.Calculation = $xlCalculationManual
$wb.close($true) #close and save
$Excel.quit()
spps -n excel
I feel like I've got a lot of code that I don't need in here as I try to find what works.
Edit-
This helped me find the initial solution. I just want to understand why it only works for some of the files.
Powershell Script - Open Excel, Update External Data, Save as

Delete first four rows in Excel with Powershell

I have a powershell script that opens an excel file, re-assigns a blank password, and then saves the file. I want to add a task to the script to remove the first 4 rows of the excel file before saving it.
You can't use OleDB for deleting data from Excel document. As per MSDN docmentation:
Although the Jet OLE DB Provider allows you to insert and update
records in an Excel workbook, it does not allow DELETE operation
What you can do is use Exel's COM interface to delete rows. Remember to release COM object too. Like so,
$file = c:\temp\MyExcelFile.xls
# Star Excel, hide window
$excel = new-object -com Excel.Application -Property #{Visible = $false}
$workbook = $excel.Workbooks.Open($file) # Open the file
$sheet = $workbook.Sheets.Item(1) # Activate the first worksheet
[void]$sheet.Cells.Item(1, 1).EntireRow.Delete() # Delete the first row
$workbook.Close($true) # Close workbook and save changes
$excel.quit() # Quit Excel
[Runtime.Interopservices.Marshal]::ReleaseComObject($excel) # Release COM

Resources