	Class AssyCOG
	Public i As Long
	
	Sub Main()
		Dim oAssyDoc As AssemblyDocument
		If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
 			MsgBox("Run in an Assembly!")
 			Return
		End If
		oAssyDoc = ThisApplication.ActiveDocument
	
		Dim oCompDef As AssemblyComponentDefinition
		oCompDef = oAssyDoc.ComponentDefinition
	
		Dim oOcc As ComponentOccurrence
		
		Dim strExcelPath As String
		strExcelPath = "C:\Users\JMÉNOUŽIVATELE\Documents\Sešit1.xlsx" 'Create this EXCEL template file before running the code
		
		oExcelApp = GoExcel.Application
		GoExcel.CellValue(strExcelPath, "List1", "A1") = "Sub-Assembly/Part Name"
		GoExcel.CellValue(strExcelPath, "List1", "B1") = "Mass [kg]"
		GoExcel.CellValue(strExcelPath, "List1", "C1") = "X"
		GoExcel.CellValue(strExcelPath, "List1", "D1") = "Y"
		GoExcel.CellValue(strExcelPath, "List1", "E1") = "Z"
		
		i=2
		
		For Each oOcc In oCompDef.Occurrences
			If Not TypeOf oOcc.Definition Is VirtualComponentDefinition Then
				Call GetCOG(oOcc, strExcelPath)
			End If
		Next
		
		GoExcel.Save
		GoExcel.Close
		MsgBox ("EXCEL table " & strExcelPath & " created, containing " & i-1 & "lines.")
	
	End Sub
	
	Function GetCOG(oOcc As ComponentOccurrence, s As String)
		 If oOcc.DefinitionDocumentType = kPartDocumentObject Then
	
			Dim oMassProps As MassProperties
			Dim oAsmbDoc As PartDocument
			
			oAsmbDoc = oOcc.Definition.Document
			oMassProps = oAsmbDoc.ComponentDefinition.MassProperties
			
			GoExcel.CellValue(s, "List1", "A" & CStr(i))= oOcc.Name
			GoExcel.CellValue(s, "List1", "B" & CStr(i))= oMassProps.Mass
			GoExcel.CellValue(s, "List1", "C" & CStr(i))= oMassProps.CenterOfMass.X
			GoExcel.CellValue(s, "List1", "D" & CStr(i))= oMassProps.CenterOfMass.Y
			GoExcel.CellValue(s, "List1", "E" & CStr(i))= oMassProps.CenterOfMass.Z
			
			i=i+1
			
		ElseIf oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then

			Dim oMassProps As MassProperties
			Dim oAsmbDoc As AssemblyDocument
			
			oAsmbDoc = oOcc.Definition.Document
			oMassProps = oAsmbDoc.ComponentDefinition.MassProperties
			
			GoExcel.CellValue(s, "List1", "A" & CStr(i))= oOcc.Name
			GoExcel.CellValue(s, "List1", "B" & CStr(i))= oMassProps.Mass
			GoExcel.CellValue(s, "List1", "C" & CStr(i))= oMassProps.CenterOfMass.X
			GoExcel.CellValue(s, "List1", "D" & CStr(i))= oMassProps.CenterOfMass.Y
			GoExcel.CellValue(s, "List1", "E" & CStr(i))= oMassProps.CenterOfMass.Z
			
			i=i+1

		End If
			
		If oOcc.SubOccurrences.Count > 0 Then
			For Each oOcc In oOcc.SubOccurrences
			 If Not TypeOf oOcc.Definition Is VirtualComponentDefinition Then
				Call GetCOG(oOcc, s)
			End If
			Next
		End If
	End Function
End Class
