Hey fellas, I am beginning with the SW API. My name is Richard. I've been programming a macro to help me to rename assemblies and each part from they. Everything was well until I opened a part (.sldprt) from the assembly. It works (the document opens) but there is an error message that says: "Object doesnt support this property or method".
This message only appears when I use "Part.OpenCompFile" to open a part (*.sldprt) I dont have any problem with the assemblies (*.sldasm). On the other hand, I've tried to use the "opendoc6" but it does not work.
The code is the following... what am I doing wrong? Thanks!
Excuse me if there are some incongruities on the code. In red and blue some notes about my problem.
'-----------------------------------------------------------------------------------------------------------
Private ruta As String
Private name As String
Private title As String
Private names(5) As String
Private x As Integer
Private ASM1 As Integer
Private ASM2 As Integer
Sub TraverseComponent(swComp As SldWorks.Component2, nLevel As Long)
Dim swModel As SldWorks.ModelDoc2
Dim vChildComp As Variant
Dim swChildComp As SldWorks.Component2
Dim sPadStr As String
Dim i As Long
vChildComp = swComp.GetChildren
Debug.Print UBound(vChildComp) 'Presenta el numero de componentes determinado por vChildComp
Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.ActiveDoc
For i = 0 To UBound(vChildComp)
Set swChildComp = vChildComp(i)
title = swModel.GetTitle ' Inicio A continuacion se adquiere el nombre del ensamble y se edita la extension del archivo
title = Replace(title, ".sldasm", "") ' Inicio A continuacion se adquiere el nombre del ensamble y se edita la extension del archivo
title = Replace(title, ".SLDASM", "") ' Inicio A continuacion se adquiere el nombre del ensamble y se edita la extension del archivo
names(x) = swChildComp.Name2
If x < 5 Then
names(x) = Replace(names(x), names(x + 1), "")
names(x) = Replace(names(x), "/", "")
name = (names(x) & "@" & title)
Debug.Print "Nombre: " & name
Else
name = (names(x) & "@" & title)
Debug.Print "Nombre: " & name
End If
ruta = swChildComp.GetPathName
ASM1 = InStr(ruta, ".sldasm")
ASM2 = InStr(ruta, ".SLDASM")
Debug.Print "Ruta: " & ruta
If ASM1 Or ASM2 <> 0 Then
x = x - 1
End If
rename
TraverseComponent swChildComp, nLevel + 1 ' PERMITE BUSCAR DENTRO DE OTRO ENSAMBLAJE
Next i
End Sub
Sub rename()
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Set swApp = CreateObject("SldWorks.Application")
Set Part = swApp.ActiveDoc
boolstatus = Part.Extension.SelectByID2(name, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
If ASM1 Or ASM2 = 0 Then
'Part.OpenCompFile ' This is for *.sldprt. Here the part (*.sldprt) opens but the error appears!
Set Part = swApp.OpenDoc6(ruta, swDocPART, 0, "", longstatus, longwarnings) ' Tried to open the *.sldprt with opendoc6, does not work... why?
Else
Part.OpenCompFile ' This is for *.sldasm, no problem.
End If
'swApp.CloseDoc "Cuerpo"
End Sub
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swConfMgr As SldWorks.ConfigurationManager
Dim swConf As SldWorks.Configuration
Dim swRootComp As SldWorks.Component2
Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.ActiveDoc
Set swConfMgr = swModel.ConfigurationManager
Set swConf = swConfMgr.ActiveConfiguration
Set swRootComp = swConf.GetRootComponent3(True)
x = 5
TraverseComponent swRootComp, 1
End Sub
'-----------------------------------------------------------------------------------------------------------
SolidworksApi macros