Code:
Sub CATMain()
If CATIA.Documents.Count > 0 Then
MsgBox "The selected doc is: " & GetSelDoc.Name
End If
End Sub'---------------------------------------------------------------------------------------
' Procedure : GetSelDoc
' Author : jherzog
' Date : 28.10.2015
' Time : 16:05
' Languages : VBA 6.5
' V5-Release: V5R19/21
' Purpose : Interactivly select a document(product, part or drawing)
' Parms : -
' Ret. Value: the selected doc object
'
' Syntax : GetSelDoc
'
' Prereqs : an open doc
' Remarks : -
'---------------------------------------------------------------------------------------
'
Function GetSelDoc() As Variant
Dim oSel 'As Selection
Dim arrSelWhich(2)
Dim Status As String
Dim oPP
Dim i As Integer
On Error GoTo GetSelDoc_Error
For i = 1 To CATIA.Documents.Count
CATIA.Documents.Item(i).Product.ApplyWorkMode (DEFAULT_MODE) 'set work mode
DoEvents
Next
Set oSel = CATIA.ActiveDocument.Selection
oSel.Clear
arrSelWhich(0) = "Part" 'needs to be part first?!
arrSelWhich(1) = "Product" 'otherwise only products will be allowed
arrSelWhich(2) = "DrawingRoot"
Status = oSel.SelectElement2(arrSelWhich, "Select Part, Product or Drawing", False)
If (Status = "Normal") Then
Set oPP = oSel.Item2(1)
Select Case oPP.Type
Case "Part"
Set GetSelDoc = oPP.Value.Parent
Case "Product"
Set GetSelDoc = oPP.Value.ReferenceProduct.Parent
Case "DrawingRoot"
Set GetSelDoc = oPP.Document
End Select
End If
oSel.Clear
Exit Function
'---------------------------------------------------------------------------------------
GetSelDoc_Error:
Dim errMsg As String
Dim errRet As VbMsgBoxResult
Select Case Err.Number
Case 438 'ApplyWorkMode causes error if already set
Resume Next
' Case -2147467259
Case Else
errMsg = Err.Number & ": " & Err.Description & " in procedure GetSelDoc"
errRet = MsgBox(errMsg, vbOKOnly, "GetSelDoc")
End Select
'Resume Next 'fall thru to quit sub
'---------------------------------------------------------------------------------------
End Function