In the attached pic you'll see how the objects options that show up for component definition is missing. However If I declare an active document and then try to set the component definition I have the option (line above). I'd much rather write
Set oCompDef = ThisApplication.ActiveDocument.CompentDefinition
than
Dim oPartDoc As PartDocument
Set oPartDoc = ThisApplication.ActiveDocument
Dim oCompDef As PartComponentDefinition
Set oCompDef = oPartDoc.ComponentDefinition
What gives, what am I doing wrong?
Solved! Go to Solution.
Solved by ekinsb. Go to Solution.
You're not doing anything wrong and neither is VBA. That's not saying that it wouldn't be nice if it did behave the way that you would like it to but because of the way things are currently defined, it's not possible. Let's look at why it's behaving the way it is.
The first thing the line below is calling is the ActiveDocument property of the Application property. If you look at the ActiveDocument property in the Object Browser or the documentation you'll see that it returns a Document object. This is a generic object that can represent any of the different types of documents since any type of document can be currently active.
ThisApplication.ActiveDocument.CompentDefinition
The next portion of the statement calls the ComponentDefinition property on the returned Document object. If you have a drawing document active when you run this program it will fail when it tries to call the ComponentDefinition property on the DrawingDocument object because it doesn't exist. If a part is active then the ActiveDocument property will return a PartDocument and calling ComponentDefinition will successfully return a PartComponentDefinition object. Calling this same line with an assembly active will return an AssemblyComponentDefinition object.
When the program is actually running, VBA is able to look and see what the ActiveDocument property is returning because it will be a specific type and not the generic Document type, but when you're writing the program it can only rely on what's defined in the type library and a Document object doesn't support the ComponentDefinition property.
An extreme example of this is below:
Dim thing as Object
Set thing = ThisApplication.ActiveDocument.SelectSet.Item(1)
thing.
The code above assumes something is selected. As long as one thing is selected it will successfully execute. It could be a feature, a work point, a sketch circle, or anything. In the next line I've typed the variable name and a ".". What should I expect to pop up in Intellisense dialog. VBA has no idea what type of object will be selected and can't know until runtime. All VBA knows at design time is that thing is of type Object.
I just took a second look at your code and it doesn't show how you declared the oCompDef variable. The following should achieve what you want. VBA now has what it needs to correctly use Intellisense with oCompDef. However when you're typing out the statement and type the period after ActiveDocument it won't show that ComponentDefinition is an available property because of what I explained above.
Dim oCompDef As PartComponentDefinition
Set oCompDef = ThisApplication.ActiveDocument.CompentDefinition