darkjmf1
Structural
- Dec 17, 2012
- 49
I have the following VBA code in a Module within Excel:
Sub Prueba_1(AttachToInstance)
'AttachToInstance = True: usa una instancia de SAP ya abierta (no abre un nuevo SAP)
'AttachToInstance = False: abre una nueva instancia de SAP
Dim ret As Long
'Path del ejecutable del SAP
Dim ProgramPath As String
ProgramPath = "C:\Program Files\Computers and Structures\SAP2000 18.1.1\SAP2000.exe"
'Path del fichero SDB
Dim ModelPath As String
ModelPath = Hoja1.txt_Fichero.Text
'Crear instancia SAP
Dim mySapObject As cOAPI
Set mySapObject = Nothing
If AttachToInstance = True Then
'Abrir modelo en una instancia de SAP ya abierta
'Obtener el objeto de SAP activo
Set mySapObject = GetObject(, "CSI.SAP2000.API.SapObject")
Else
'Crear una nueva instancia de SAP
'Crear objeto OAPI helper
Dim myHelper As cHelper
Set myHelper = New Helper
'Obtener el objeto SAP
Set mySapObject = myHelper.CreateObject(ProgramPath)
'Iniciar aplicación
mySapObject.ApplicationStart
End If
'Obtener una referencia a cSapModel para acceder a todas las clases y funciones de OAPI
Dim mySapModel As cSapModel
Set mySapModel = mySapObject.SapModel
'Inicializar modelo
ret = mySapModel.InitializeNewModel
'Abrir modelo de SAP definido en el cuadro de texto
ret = mySapModel.File.OpenFile(ModelPath)
....
End Sub
If I try to work in an already open instance of SAP (with AttachToInstance=True) it works like a charm.
However if I try to open a new instance (AttachToInstance=False) I get an error "ActiveX component can't create object" when running "Set myHelper = New Helper".
I can't see where the problem is, as long as the "CSI_OAPI_Documentation_v18.hlp" says it has to be done that way, including some examples.
Of course I have included the reference to SAP2000v18.TLB in Excel, in case anybody has this doubt.
Any help would be really appreciated.
Thanks.
Sub Prueba_1(AttachToInstance)
'AttachToInstance = True: usa una instancia de SAP ya abierta (no abre un nuevo SAP)
'AttachToInstance = False: abre una nueva instancia de SAP
Dim ret As Long
'Path del ejecutable del SAP
Dim ProgramPath As String
ProgramPath = "C:\Program Files\Computers and Structures\SAP2000 18.1.1\SAP2000.exe"
'Path del fichero SDB
Dim ModelPath As String
ModelPath = Hoja1.txt_Fichero.Text
'Crear instancia SAP
Dim mySapObject As cOAPI
Set mySapObject = Nothing
If AttachToInstance = True Then
'Abrir modelo en una instancia de SAP ya abierta
'Obtener el objeto de SAP activo
Set mySapObject = GetObject(, "CSI.SAP2000.API.SapObject")
Else
'Crear una nueva instancia de SAP
'Crear objeto OAPI helper
Dim myHelper As cHelper
Set myHelper = New Helper
'Obtener el objeto SAP
Set mySapObject = myHelper.CreateObject(ProgramPath)
'Iniciar aplicación
mySapObject.ApplicationStart
End If
'Obtener una referencia a cSapModel para acceder a todas las clases y funciones de OAPI
Dim mySapModel As cSapModel
Set mySapModel = mySapObject.SapModel
'Inicializar modelo
ret = mySapModel.InitializeNewModel
'Abrir modelo de SAP definido en el cuadro de texto
ret = mySapModel.File.OpenFile(ModelPath)
....
End Sub
If I try to work in an already open instance of SAP (with AttachToInstance=True) it works like a charm.
However if I try to open a new instance (AttachToInstance=False) I get an error "ActiveX component can't create object" when running "Set myHelper = New Helper".
I can't see where the problem is, as long as the "CSI_OAPI_Documentation_v18.hlp" says it has to be done that way, including some examples.
Of course I have included the reference to SAP2000v18.TLB in Excel, in case anybody has this doubt.
Any help would be really appreciated.
Thanks.