Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Macro functionning for assembly or part

TONELLAL
Collaborator

Macro functionning for assembly or part

TONELLAL
Collaborator
Collaborator

Hello,

I'm trying to create a macro functionning for all parts if the active document is an assembly, or on the part if it is a part. Something like this :

 

Dim oRefDocs As DocumentsEnumerator
Dim oRefDoc As Document
Dim odoc As Document

 

Set odoc = ThisApplication.ActiveDocument

If odoc.DocumentType = kAssemblyDocumentObject Then Set oRefDocs = odoc.AllReferencedDocuments

If odoc.DocumentType = kPartDocumentObject Then Set oRefDocs = odoc

 

For each oRefDoc in oRefDocs

...

next

 

The red line does not function, and I'm stuck... any idea ?

0 Likes
Reply
Accepted solutions (1)
494 Views
5 Replies
Replies (5)

WCrihfield
Mentor
Mentor

What are you attempting to use that document variable for later in your macro?  You may need to define it as its true document type (PartDocument or AssemblyDocument) before it will allow certain actions/access related to it.

Here is one approach (of many):

Dim oDocType As DocumentTypeEnum
Set oDocType = ThisApplication.ActiveDocumentType
If oDocType = kPartDocumentObject Then
	Dim oPDoc As PartDocument
	Set oPDoc = ThisApplication.ActiveDocument
	GoTo PartStart '(place in code dealing with PartDocument stuff)
ElseIf oDocType = kAssemblyDocumentObject Then
	Dim oADoc As AssemblyDocument
	Set oADoc = ThisApplication.ActiveDocument
	GoTo AsmStart '(place in code dealing with AssemblyDocument stuff)
End If

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE' :thumbs_up:.

If you have time, please... Vote For My IDEAS :light_bulb:or you can Explore My CONTRIBUTIONS

Inventor 2021 Help | Inventor Forum | Inventor Customization Forum | Inventor Ideas Forum

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

TONELLAL
Collaborator
Collaborator

I agree with your approach. To be more specific, I currently have an iLogic rule that works for iam. This rule is a treatment for all components of the iam. I'm trying to adapt it so that it works the same way for one part. The current code is of the type : 

 

oRefDocs = ThisDoc.Document.AllReferencedDocuments
For Each oRefDoc In oRefDocs
...
Next oRefDoc

 

 What I'm trying to do is to make oRefDocs contain AllreferencedDocuments if the file is an iam, or contain the file itself if it is a part.

0 Likes

J-Camper
Advisor
Advisor
Accepted solution

The DocumentsEnumerator Object can't be set to a single document from an open part file.  You could use the Application.Documents.VisibleDocuments, but you would then have to remove those you don't want if more than 1 document is open. 

 

Alternatively, if you use an ObjectCollection Object , then you could fill it with AllReferencedDocuments from assembly files or with the "odoc" by itself if it is a part.

TONELLAL
Collaborator
Collaborator

"The DocumentsEnumerator Object can't be set to a single document from an open part file."

Yes, that's the problem...

 

"Alternatively, if you use an ObjectCollection Object "

That's the workaround I use. I thought there was a better solution. The DocumentsEnumeratorObject contains Documents, ActiveDocument is a document, so I don't understand why I can't fill a DocumentsEnumeratorObject with a Document...

0 Likes

J-Camper
Advisor
Advisor

The DocumentsEnumerator Object can't be filled because it has no method for Add/Remove.  The only properties it has are: Count, Item, & Type.  You have to set it to another DocumentsEnumerator Object, like AllReferencedDocuments, in order for it to be "filled". 

0 Likes