Workbook.SaveAs method is popping Save As dialog box - excel

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)

Related

Update Excel Data with Password in PowerShell

I need open a excel file with password, update, save and close.
I try this code, but i havent sucessed.
Anyone Can help me?
#Set the file path (can be a network location)
$filePath = "C:\Users\12291\Desktop\Excel\Base Vendas 2020_NOVA.xlsx"
#Create the Excel Object
$excelObj = New-Object -ComObject Excel.Application
#Make Excel visible. Set to $false if you want this done in the background
$excelObj.Visible = $true
$updatelinks = 3D
#Open the workbook
$workBook = $excelObj.Workbooks.Open($filePath, $updatelinks,$false, 0,5 , "vendas")
#Focus on the top row of the "Data" worksheet
#Note: This is only for visibility, it does not affect the data refresh
#$workSheet = $workBook.Sheets.Item("Data")
#$workSheet.Select()
#Refresh all data in this workbook
$workBook.RefreshAll()
#Save any changes done by the refresh
$workBook.Save()
#Uncomment this line if you want Excel to close on its own
#$excelObj.Quit()

Combining Multiple Workbooks Into One Workbook Worksheet With Powershell

I have this Powershell script i'm trying to combine multiple workbooks with single sheets onto one workbook with a single sheet and combine them all on the one sheet. I can't get past the fact it keeps telling me there is no file named $destfile and can't be opened. What is the correct syntax for that?
Thanks
$ExcelObject = New-Object -ComObject excel.application
$ExcelObject.visible=$true
$file1 = 'file1location'
$file2 = 'file2location'
$destfile = 'fileI want to saveas afterits compiled'
$xl = new-object -c excel.application
$xl.displayAlerts = $false # don't prompt the user
$wb1 = $xl.workbooks.open($file1, $null, $true) # open source, readonly
$wb2 = $xl.workbooks.open($file2, $null, $true)
$wb3 = $xl.workbooks.open($destfile) # open target
$sh1_wb2 = $wb2.sheets.item(1) # first sheet in destination workbook
$sheetToCopy = $wb1.sheets.item('Sheet1') # source sheet to copy
$sheetToCopy.copy($sh1_wb2) # copy source sheet to destination workbook
$wb1.close($false) # close source workbook w/o saving
$wb2.close($true) # close and save destination workbook
$xl.quit()
spps -n excel
You can try to use https://github.com/dfinke/ImportExcel
Export data to csv from multiple worksheets with Import-CSV, then combine those CSV files (i suppose that they have identical rows) and import combined CVS back to excel using Export-CSV... quite simple. and does not require any COM manipulations.

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

Import Data from Powershell object into Excel Spreadhseet

I have built a powershell object in a script. In this same script I need to take that object (or parts of it) and load them into an Excel spreadsheet. The excel spreadsheet is already saved and has headers. I am able to find my .xlsx file, open it, save and close but not sure how to actually get my object into the file. This script will be run a lot so ultimately I need the data to just append to the first available/empty row.
Here is the script that is opening the excel file:
$excel_file_path = 'C:\Users\ME\Documents\Test\SFTPTest.xlsx'
# Instantiate the COM Object
$Excel = New-Object -ComObject Excel.Application
$Excel.Application.Visible = $true
$Excel.DisplayAlerts = $false
$ExcelWorkBook = $Excel.Workbooks.Open($excel_file_path)
$ExcelWorkSheet = $Excel.Worksheets.item("SFTPTransfers")
$ExcelWorksheet.activate() | Out-Null
The headers in my excel spreadsheet are on row 1 and there are 9 columns (A-I)
My PS Object is a large oject converted from a JSON object. I only need 9 properties of this object to go into this spreadsheet. For example, I have a property called Object1.ServerName that needs to go into the my excel file under ServerName column...etc.

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