AddComponent4 - Random behaviour?

Hi,

I am currently creating a sub routine that inserts parts into thecurrent open assembly (PVAssembly As ModelDoc2 object), below isthe code. There are a few other parts that need to be implementthat haven't yet so disregard them (i.e. the MateTo and MateSide).The problem I am having is that for the parts I am working withthis works great except for one part. The part is inserted in someassemblies but not in others, the error message I get is:

"Attempted to read or write protected memory. This is often anindication that other memory is corrupt."

I'm at a lost and can't figure it out, any suggestions? All otherparts are implemented is a similar fashion and the part file is notcorrupt.

 

Public Sub addcomponenttoassembly(ByRef componentname As String,ByRef Config As String, ByRef MateTo As String, ByRef MateSide AsString)
Dim Errors As Long
Dim model As ModelDoc2
Dim fileerror As Long
Dim filewarning As Long
Dim swComp As SldWorks.Component2
On Error GoTo Hanndler
model = iSwApp.OpenDoc6(componentname,swDocumentTypes_e.swDocPART,swOpenDocOptions_e.swOpenDocOptions_Silent, Config, fileerror,filewarning)
iSwApp.DocumentVisible(False, swDocumentTypes_e.swDocPART)
iSwApp.ActivateDoc2(AssemblyTitle, True, Errors)
swComp = PVAssembly.AddComponent4(componentname, "Default", 0, 0,0)
iSwApp.CloseDoc(componentname)

'If MateTo = "" Then

'Else

'End If
Exit Sub
hanndler:
MsgBox(Err.Description)
MsgBox(PVAssembly.GetPathName)
MsgBox(componentname)
End Sub

SolidworksApi macros