We are unable to reproduce the same behaviour you described above. If we select a message with attached document reference, the code above return the subject of the document. Here are the steps we followed:
- Select a document reference and forward as an attachment
- Select the message with document reference attached
- Run the sample code from message toolbar.
If the document is corrupt then you may get the error to access the subject, can you try the sample code with different document reference. Please let us know if we missed any steps.