<!--
{
  "availability" : [
    "macOS: 13.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/EnvironmentValues/openDocument",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Property",
  "symbol" : {
    "kind" : "Instance Property",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI17EnvironmentValuesVAAE12openDocumentAA04OpenF6ActionVvp"
  },
  "title" : "openDocument"
}
-->

# openDocument

An action in the environment that presents an existing document.

```
var openDocument: OpenDocumentAction { get }
```

## Discussion

Use the `openDocument` environment value to get the instance of the
[`OpenDocumentAction`](/documentation/SwiftUI/OpenDocumentAction) structure for a given [`Environment`](/documentation/SwiftUI/Environment). Then call
the instance to present an existing document. You call the instance
directly because it defines a [`callAsFunction(at:)`](/documentation/SwiftUI/OpenDocumentAction/callAsFunction(at:))
method that Swift calls when you call the instance.

For example, you can create a button that opens the document at the
specified URL:

```
struct OpenDocumentButton: View {
    var url: URL
    @Environment(\.openDocument) private var openDocument

    var body: some View {
        Button(url.deletingPathExtension().lastPathComponent) {
            Task {
                do {
                    try await openDocument(at: url)
                } catch {
                    // Handle error
                }
            }
        }
    }
}
```

The above example uses a `do-catch` statement to handle any errors
that the open document action might throw. It also places the action
inside a task and awaits the result because the action operates
asynchronously.

To present an existing document, your app must define a
[`DocumentGroup`](/documentation/SwiftUI/DocumentGroup) that handles the content type of the specified file.
For a document that’s already open, the system brings the existing
window to the front. Otherwise, the system opens a new window.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)