MATLAB COM Builder | ![]() ![]() |
Integrating the Component with Visual Basic for Applications
Having built your component, you can implement the necessary VBA code to integrate it into Excel. Follow these steps to open Excel and select the libraries you need to develop the add-in:
Creating the Main VB Code Module For the Application
The add-in requires some initialization code and some global variables to hold the application's state between function invocations. To achieve this, implement a Visual Basic code module to manage these tasks, as follows:
Name
property to FourierMain
. See the next figure.
Figure 4-5: VBA Project: Insert->Module
FourierMain
module:
' ' FourierMain - Main module stores global state of controls ' and provides initialization code ' Public theFourier As Fourier.Fourier 'Global instance of Fourier object Public theFFTData As MWComplex 'Global instance of MWComplex to accept FFT Public InputData As Range 'Input data range Public Interval As Double 'Sampling interval Public Frequency As Range 'Output frequency data range Public PowerSpect As Range 'Output power spectral density range Public bPlot As Boolean 'Holds the state of plot flag Public bInitialized As Boolean 'Module-is-initialized flag Private Sub LoadFourier() 'Initializes globals and Loads the Spectral Analysis form Dim MainForm As frmFourier On Error GoTo Handle_Error Call InitApp Set MainForm = New frmFourier Call MainForm.Show Exit Sub Handle_Error: MsgBox (Err.Description) End Sub Private Sub InitApp() 'Initializes classes and libraries. Executes once 'for a given session of Excel If bInitialized Then Exit Sub On Error GoTo Handle_Error If theFourier Is Nothing Then Set theFourier = New Fourier.Fourier End If If theFFTData Is Nothing Then Set theFFTData = New MWComplex End If bInitialized = True Exit Sub Handle_Error: MsgBox (Err.Description) End Sub
![]() | Building the Component | Creating The Visual Basic Form | ![]() |