two issues here:
1) the occurrence name contains ‘ : ‘, which is not accepted as a char in file name. This is Windows rule. So e.g.
myPart:1.stp is truncated as myPart.
That is why you saw the file name without extension name. And the file size is 0
2) ControlDefiniton.Execute is Post Command (asynchronously). That means it will be executed after the function which invokes it. So to Send Command (Synchronous), you need to use Execute2(true)
Please refer to the code below. note: you will need to write a method youeself to filter the invalid chars before saving.
Private Sub test( )
Dim inventorAppType As Type = System.Type.GetTypeFromProgID("Inventor.Application")
Dim _invApp As Inventor.Application = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")
If _invApp.Documents.Count = 0 Then
MsgBox("Need to open an Assembly document")
Return
End If
If _invApp.ActiveDocument.DocumentType <> _
DocumentTypeEnum.kAssemblyDocumentObject Then
MsgBox("Need to have an Assembly document active")
Return
End If
Dim asmDoc As AssemblyDocument
asmDoc = _invApp.ActiveDocument
If asmDoc.SelectSet.Count = 0 Then
MsgBox("Need to select a Part or Sub Assembly")
Return
End If
Dim selSet As SelectSet
selSet = asmDoc.SelectSet
Dim compOcc As ComponentOccurrence
For Each obj In selSet
If TypeOf obj is ComponentOccurrence then
compOcc = obj
If TypeOf compOcc.Definition.Document Is PartDocument Then
Dim oCtrlDef As ControlDefinition
'isolate
oCtrlDef = _invApp.CommandManager.ControlDefinitions.Item("AssemblyIsolateCmd")
oCtrlDef.Execute2(true)
Dim oDoc As Document
' it is still assembly context. So active document is still the assembly
'oDoc = _invApp.ActiveDocument
' get the corresponding part document.
oDoc = compOcc.Definition.Document
Dim oFilename As String
' because ': ' is not accepted,
' you need to write a function to filter the char
' for simple test, i use the part file name
'oFilename = compOcc.Name & ".stp"
oFilename = oDoc.DisplayName & ".stp"
Dim oFilepath As String
oFilepath = "c:\temp\"
Dim oPathName As String
oPathName = oFilepath & oFilename
'export step
Call oDoc.SaveAs(oPathName, True)
'undo isolate
oCtrlDef = _invApp.CommandManager.ControlDefinitions.Item("AssemblyIsolateUndoCmd")
oCtrlDef.Execute2(true)
End If
End If
Next
End Sub