Cant Get Document from DocumentClosing hook

I am using a doc-closing.py hook to try and get a document Name.

But it seems to me that the document name is being released from memory before it can be returned, which results in pyrevit displaying this message:

image

is there another way to get a document name of a file that is being closed?
I have also tried using DocumentId property but it results in the same warning.

Below is my code:

import clr
import os
clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
from Autodesk.Revit.DB import *
from datetime import datetime

doc = __eventargs__.Document

log_file_path = '...\\ProjectLogs.txt'

now = datetime.now()
date_time = now.strftime("%Y/%m/%d, %H:%M:%S")

log_entry = (
    date_time + "\t" +
    os.getenv('username') + "\t" +
    "Closed" + "\t" +
    str(doc.Title) + "\t" + "\n"
).encode('utf-8')

with open(log_file_path, "ab") as file:
    file.write(log_entry)

I’d try the doc-closing event instead (Document Events)
instead of the doc-closed from the Application Events as it can give you the DocumentId but the Document object does not exist anymore.

I am using doc-closing.py and not doc-closed.py

But it seems it was a typical case of “have you tried turning if off and back on again”
I restarted revit, and its now working without any changes.

It would be interesting to find out why it happened though if it ever happens again in future.

That cannot find target file maybe deleted was it not able to find the hook file even though it was there. I found a restart is needed sometimes when messing with the hooks. Reload don’t always do it.

try renaming the hook and then close the document to test and you should get that error again.

1 Like

Spot on. Thanks @jpitts