Save selected elements to a new project file, copy Project cordinates and relink the file

Hi - im trying to create a macro type function in Dynamo. I want the ability to create elements in my main model but then send them to a new revit file with same PBP and shared coords, save and relink the file in to the original model, allowing me to delete the originals. Purpose is to offload categories to linked models to reduce the file size for Central storage.

This is the script i’m working on, but i keep geeting the Unexpected token error. Ive checked the code syntax but there doesnt seem to be any errors i can see- tearing my hair out!!

Anyone help?

What is the specific error text?

Also please share the source code using the pre formatted text option so that people who want to help don’t have to retype your code from an image.

import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
# Input variables
selectedElements = IN[0]  # List of selected elements to export
# Start Dynamo transaction
doc = DocumentManager.Instance.CurrentDBDocument
TransactionManager.Instance.EnsureInTransaction(doc)
# Create a new document for exporting selected elements
newDoc = doc.Application.NewProjectDocument(UnitSystem.Metric)
newDoc.ProjectInformation.Name = "Exported Model"
copyOptions = CopyPasteOptions()
copyOptions.SetDuplicateTypeNamesHandler(CopyDuplicateTypeNamesHandler())
elementIds = []
for elem in selectedElements:
    elementIds.append (ElementTransformUtils.CopyElement(doc, elem.Id, newDoc, Transform.Identity, copyOptions))
# Set the same project coordinates, levels, grids, and project base point
ElementTransformUtils.CopySharedCoordinates(doc, newDoc)
newDoc.ProjectBasePoint = doc.ProjectBasePoint
newDoc.ProjectLocation = doc.ActiveProjectLocation
newDoc.ActiveView = newDoc.GetElement(doc.ActiveView.Id)
# Save and close the new document
newDoc.SaveAs("C:\\Users\\user\\Documents\\exported model.rvt")
newDoc.Close()
# End Dynamo transaction
TransactionManager.Instance.TransactionTaskDone()
# Relink the exported model back to the existing project
TransactionManager.Instance.EnsureInTransaction(doc)
relinkOptions = RelinkOptions(True)
RelinquishOptions = RelinquishOptions(True)
doc.RelinquishOwnership(relinkOptions, RelinquishOptions)
doc.LoadFamily("C:\\Users\\user\\Documents\\exported model.rvt")
TransactionManager.Instance.TransactionTaskDone()
# Output the exported model file path
OUT = "C:\\Users\\user\\Documents\\exported model.rvt"

Apologies - its late here!

Original error:
warning-ironpythonevaluator-evaluateironpython scriptoperation failed unexpected token newline

I did some checking and seen that the filepaths may have needed double backslashes
When i changed this - i get this error:

Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File “”, line 16, in
Exception: Attempt to modify the model outside of transaction.

You’ll need to create a transaction in the new document, commit it, and then create a transaction transaction in the original document. Look into the transaction class for insights on how, there are also a lot of examples on the forum.