Skip to main content

Search OfficeIMO

Enter a topic, API type, or PowerShell command.

Documentation Search all OfficeIMO/
Cmdlet

Close-OfficeExcel

Module
PSWriteOffice
Inputs
OfficeIMO.Excel.ExcelDocument

Closes a live Excel workbook without saving pending changes.

Syntax

Close-OfficeExcel
[-Document] <ExcelDocument>
[<CommonParameters>]
#
Parameter set: Default

Description

Use Save-OfficeExcel before closing to retain changes. Workbooks with SaveOnDispose are rejected so disposal cannot publish a file.

Examples

Save edits explicitly, then close.

$workbook | Save-OfficeExcel
$workbook | Close-OfficeExcel

Close a workbook without saving

$book = Open-OfficeExcel -Path .\Reports\Q3-Sales.xlsx -ReadOnly
Close-OfficeExcel -Document $book

Releases a workbook that was only read; nothing is written to disk.

Always close after reading

$book = Open-OfficeExcel -Path .\Finance\Purchase-Requests.xlsx -ReadOnly
try {
    Import-OfficeExcel -Document $book -Table Requests | Where-Object Status -eq 'Awaiting approval'
} finally {
    Close-OfficeExcel -Document $book
}

Reads the open purchase requests and closes the workbook in a finally block, so the file is released even if the filter fails.

Parameters

-Document ExcelDocument requiredposition: 0pipeline: True (ByValue)
Explicit-persistence workbook to close.

Common Parameters

This command supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable.

For more information, see about_CommonParameters.