Thursday, October 01, 2009

Retrieving currently open documents using Application.Documents

In the code below (which I recommend you don't use until you've read this whole post!) I'm passing a function a known filename and am asking Revit to find it and return it:

Public Shared Function GetFileByName(ByVal name As String) As Document

  Dim docIterator As DocumentSetIterator
        docIterator = revitApp.Documents.GetEnumerator
        Dim doc As Document = Nothing

        While docIterator.MoveNext()
            doc = docIterator.Current
            If doc.Title.ToString = name Then
                Return doc
            End If


        End While

Return (Nothing)

End Function

I recently had a live release failing on a user and after a bit of poking around I found that this code was returning nothing, but we knew that the document was open. It was failing because Revit wasn't returning the file extensions. It only happened on a couple of machines on this particular site, whereas others were fine. To my surprise it was because of the 'Hide extensions for known file types' user setting in Windows Explorer:



So, when you're doing something similar, be sure to allow for this setting being both on and off. I modified my code thus:
If (doc.Title.ToString = name) Or (doc.Title.ToString & ".rvt" = name) Then

0 comments:

Post a Comment

Comments are moderated so you may have to wait a while before your comment appears :)

Thanks for your contribution!