Cannot Call A Method On A Null (Excel WorkSheet) - excel

I'm currently trying to grab a specific worksheet from an Excel file in PowerShell and I'm receiving the error "You cannot call a method on a null-valued expression". PowerShell picks up the workbook and even that the property Worksheet exists, yet it does not recognize item(1).
$excel = "C:\budget.xlsx"
$ex = New-Object -ComObject Excel.Application
$ex.Visible = $true
$wb = $ex.Workbooks.Open($excel)
$ws = $wb.Worksheets.item(1) # This line errors
When accessing Excel worksheets, this is the syntax (for instance, see this code, and this is just one of many examples). Is there something else that needs to be loaded in order to do this? After searching the 'net, I can't find anyone else loading or using another syntax.

Related

Trying to delete an excel worksheet and recreate it, but throws an error that the name already exists

Edit: Ended up finding out what was causing it, but unsure why this works. The fix was to add this line after the excel object was created:
$excel.DisplayAlerts = $False
I'm using PowerShell and trying to delete an existing excel worksheet and then recreate it. My environment is locked down for modules, so unfortunately I can't use ImportExcel or other similar modules. I'm a bit new to PowerShell so apologies if the code is messy, but here's what I currently have:
$TodaysDate = Get-Date -Format "yyyy-MM-dd"
# Open a new excel object
$excel = New-Object -ComObject excel.application
$excel.visible = $False
# If a workbook does not already exist, create one
# If a workbook does exist, open it.
if (-not(Test-Path -Path $ExistingExcelFile -PathType Leaf)) {
$workbook = $excel.Workbooks.Add()
# Delete the automatically generated sheet
$workbook.WorkSheets.Item("Sheet1").Delete()
} else {
$workbook = $excel.Workbooks.Open($ExistingExcelFile)
}
# If a worksheet with todays date already exists, delete it so we can start fresh
if (($TodaysDate -in $($workbook.Worksheets).Name)) {
$TodaysWrkSheet = $workbook.WorkSheets.Item($TodaysDate)
$TodaysWrkSheet.Activate()
$TodaysWrkSheet.Delete()
}
#Add the worksheet and set the name
$WrkSheet = $workbook.Worksheets.Add()
$WrkSheet.Name = $TodaysDate
# If the summary page exists, delete it so we can start fresh
if (("Summary" -in $($workbook.Worksheets).Name)) {
$SummaryWrkSheet = $workbook.WorkSheets.Item("Summary")
$SummaryWrkSheet.Activate()
$SummaryWrkSheet.Delete()
}
$SummaryPage = $workbook.Worksheets.Add()
$SummaryPage.Name = "Summary"
This is the error I get when it tries to create the new sheet with todays date:
That name is already taken. Try a different one.
The second part with the summary page works fine and it finds/deletes/recreates it, so I'm assuming it's a problem with $TodaysDate, but why? The if statement block of code for ($TodaysDate -in $($workbook.Worksheets).Name) gets hit, would there be a problem with $TodaysWrkSheet = $workbook.WorkSheets.Item($TodaysDate)? It creates the sheet just fine as long as it doesn't exist already, so it at least doesn't complain creating a sheet name with $TodaysDate. I've been trying for a while to figure out why but haven't been having any luck.
Any help is appreciated.

Paste\Return into Cell A1 of open Excel sheet

I wish to take a string generated in PowerShell and paste\return that string into Cell A:1 of an OPEN spreadsheet. I have researched this but I dont understand the excel specific syntax.
Do I need to identify and declare the PID for the specific Excel workbook thats running and if so how?
I know how to create a new workbook and add the text to cell A:1 but not one which is already open. It must also "press return" to execute the spreadsheet functions once pasted.
I dont know where to start other than: ((new-object -com "excel.application").Workbooks.Add()).application.Visible=$True
I have scoured the web but have not found any examples that make sense. Please help
You can achieve it by using the below script
#Specify the path of the excel file
$FilePath = "path\test.xlsx"
#Specify the Sheet name
$SheetName = "Sheet1"
# Create an Object Excel.Application using Com interface
$objExcel = [Runtime.Interopservices.Marshal]::GetActiveObject('Excel.Application')
# Disable the 'visible' property so the document won't open in excel
$objExcel.Visible = $false
# Open the Excel file and save it in $WorkBook
$WorkBook = $objExcel.Workbooks.Open($FilePath)
# Load the WorkSheet 'BuildSpecs'
$WorkSheet = $WorkBook.sheets.item($SheetName)
$WorkSheet.Cells.Item(1,1) = "Link" #Updates the first cell (A1)
$WorkBook.Save()
$WorkBook.Close()
Make sure the file has write permissions from PowerShell scripts.
If you want to edit the sheet which is already open, change the path of the file and mention the sheet name properly and it would work as expected.

PowerShell + Excel.Application add sheets problem

I am trying to implement a script to update some Excel sheets/cells.
For this I am using Windows PowerShell ISE with the following code:
# open application
$document = New-Object -ComObject excel.application
$document.Application.Visible = $true
$document.DisplayAlerts = $false
# Create workbook
$workbook = $document.Workbooks.Add()
After this, I can use the following command to see which sheets I have:
$workbook.Sheets | Select-Object -Property Name
And it works perfectly. The problem is when I add a new sheet with the following:
$workbook = $document.Sheets.Add()
It creates the new sheet, but… when I use the command to see the sheet names, it does not show anything, looks like the Sheets.Add() crashes something…
Can someone help me with this topic? Am I doing something wrong?

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.

Adding Same Column Header Names to all worksheets in an Excel Book

I have a script that will create worksheets based on the number of files that it finds in a directory. From there it changes the name of the sheets to the file name. During that process, I am attempting to add two Column header values of "Hostname" and "IP Address" to every sheet. I can achieve this by activating each sheet individually but this becomes rather cumbersome as the amount of sheets goes past 20+ and thus I am trying to find a dynamic way of doing this regardless the amount of sheets that are present.
This is the code that I have to do everything up to the column header portion:
$WorksheetCount = (Get-ChildItem $Path\Info*.txt).count
$TabNames = Get-ChildItem $Path\Info*.txt
$NewTabNames = Foreach ($file IN $TabNames.Name){$file.Substring(0,$file.Length-4)}
$Break = 0
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$Workbook = $Excel.Workbooks.Add()
$null = $Excel.Worksheets.Add($MissingType, $Excel.Worksheets.Item($Excel.Worksheets.Count),
$WorksheetCount - $Excel.Worksheets.Count, $Excel.Worksheets.Item(1).Type)
1..$WorksheetCount
Start-Sleep -s 1
ForEach ($Name In $NewTabNames){
$Break++
$Excel.Worksheets.Item($Break).Name = $Name
}
I have attempted to insert my code as such:
ForEach ($Name In $NewTabNames){
$Break++
$Excel.Worksheets.Item($Break).Name = $Name
$cells=$Name.Cells
$cells.item(1,1)="Hostname"
$cells.item(1,2)="IP Address"
}
When I attempt to run the script, I get the following error..
You cannot call a method on a null-valued expression.
And then it proceeds to list each line of the code that I had put in. I thought that since I created a variable during the operation, that it was the issue:
$cells=$Name.Cells
I thought That perhaps if I moved it before the ForEach command that it would resolve it but I still receive the same issue. I have looked through various ways of trying to select ranges of sheets within excel via powershell but have not found anything helpful.
Would appreciate any assistance on this.
This is actually my first post in StackOverflow ever and I feel pretty excited to finally help out. I made some small modifications to your code and seems to work fine. I noticed some odd behavior when I removed the $null variable that was getting assigned because it seemed strange to me why it was being done, but after removing that assignment my outlook application open by itself automatically every time I ran the script. I found the site where you got the code from just to see if there were any changes to the original code.
I found this Microsoft documentation very helpful to figure this out.
This is what I modified
ForEach ($Name In $NewTabNames){
$Break++
$Excel.Worksheets($Break).Name = $Name
$Excel.Worksheets($Break).Cells(1,1).Font.Bold = $true
$Excel.Worksheets($Break).Cells(1,1) = "Hostname"
$Excel.Worksheets($Break).Cells(1,2).Font.Bold = $true
$Excel.Worksheets($Break).Cells(1,2) = "IP Address"
}

Resources