3445x
001293
2016-08-02

Interface COM no VBA | 1. Abra o RFEM, crie e guarde o modelo, feche o RFEM

A primeira parte do artigo sobre a interface COM descreve como abrir e fechar o RFEM. Als Programmiersprache wird VBA in Excel verwendet, der Programmablauf ist aber identisch zur Programmierung mit C#. Damit VBA die Befehle zur Schnittstelle kennt, muss zunächst der entsprechende Verweis hinzugefügt werden. In der Abbildung ist links das Beispiel anhand von RFEM 5 zu sehen.

Então, o código fonte para um programa simples. Pode criar primeiro a interface para o modelo e depois abrir o programa utilizando o modelo. Após guardar, o programa é fechado.

É sempre bom utilizar o manipulador de erros para detectar e exibir os possíveis problemas de entrada / saída. Se ocorrer um problema, a licença será novamente desbloqueada para poder aceder ao RFEM. Mais explicações podem ser encontradas no código-fonte.

Sub CreateModel()

' Firstly, an interface
' to a new model is created.
Dim iModel As RFEM5.model
Definir iModel = Novo RFEM5.model

' Here, the model name is defined,
' either the content of cell B2 of sheet Table1
' or, if empty, "test.rf5".
Dim modelName As String

If IsEmpty(Worksheets( "Table1").Range( "B2").Value) Then
modelName = "test01.rf5"
Else
modelName = CStr(Worksheets( "Tabelle1").Range( "B2").Value)
End If

' Transfer of the model name to the interface.
iModel.SetName(modelName)

' It is possible to specify a model description here.
iModel.SetDescription( "description")

' Error handling routine.
On Error GoTo e

Dim iApp As RFEM5.Application
' Interface to the program is opened.
' (Program is started.)
Definir iApp = iModel.GetApplication

' COM license and program access are blocked.
iApp.LockLicense

' Program is displayed in the foreground.
iApp.Show

' Model is saved at "C:\temp" .
iModel.Save("C:\temp\" & modelName)

e: If Err.Number <> 0 Then MsgBox Err.Description, , Err.Source

' COM license is unblocked, program access possible again.
iModel.GetApplication.UnlockLicense
' Program is closed.
iApp.Close

End Sub


Autor

O Eng. Günthel opera na área do apoio técnico para clientes.

Ligações