Basically, any time a rule is for a specific type of document, and it is possible for that rule to be ran on any other type of document, you should always check the Type of document you are getting a reference to, before trying to set that document reference to any of the 'specific' document Type variables (AssemblyDocument, DrawingDocument, PartDocument). If you try to check document type afterwards, the error will have usually already happened. If you try to set an AssemblyDocument as the value of a DrawingDocument type variable, it will throw an error. There are several popular ways to deal with that situation, depending on the situation, and your individual coding style.
Below are a few starting lines examples I often use:
If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisDoc.Document
or
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
or
If Not TypeOf ThisDoc.Document Is AssemblyDocument Then Exit Sub
Dim oADoc As AssemblyDocument = ThisDoc.Document
There are lots of other similar examples too. Whichever suits your needs & style best.
Wesley Crihfield

(Not an Autodesk Employee)