Using VBA

<< Click to Display Table of Contents >>

Navigation:  Working with CanEasy > Automation in CanEasy > VBA >

Using VBA

 

To access bus objects via VBA, a workspace must be loaded or a bus must be generated.

Additionally, a macro project must be created before starting programming,. Multiple macro projects can be handled simultaneously and each macro project can contain several macros.

 

 

Open the VBA development environment

The development environment for VBA (VBA IDE) can be launched by pressing ALT+F11, from the “Tools” menu or the context menu of the VBA tree entry. Before the VBA IDE can be used, a new project must be created beneath the VBA node in the CanEasy tree. Within the project, all public procedures without parameters are displayed (in other words: all macros).

 

 

Open the macros dialog

The macro dialog is part of the VBA IDE. Via this dialog, new macros can be created and existing macros started. The macro dialog can be opened by pressing ALT+F8, from the “Tools” menu, or the context menu of the VBA tree entry.

 

 

Create a macro project

Select the menu entry “New” from the context menu of the VBA root node. The VBA editor is opened and the macro project “VBAProject1” is created in the tree view. The macro project can be renamed and saved using its context menu (renaming is also possible inside the VBA editor).

 

 

Create macro

Macros can be created in the VBA editor. The following code gives an example of how macros are treated within CanEasy:

 

Sub FirstMacro ()

'Show messagebox with text

MsgBox “hello world!”

End Sub

 

VBA macro in the treeview
 
After entering the sample code in the VBA editor (see right figure), the macro name is displayed (see top figure) in the tree view of CanEasy within the macro project. Additional macros are displayed within the macro project as well.

 

VBAEditor1

 

In the tree view, CanEasy displays macros or sub-routines created as modules inside the VBA editor. Class modules and forms (UserForm) are displayed only in the VBA editor.

 
 

Execute macros

Each macro can be started directly from its corresponding context menu. Likewise, macros can be started from the context menu of the macro project. If only one macro is available in the macro project, it is started directly. If several macros are available, a list is displayed from which the desired macro can be selected. Macros can also be started from the VBA editor.

On starting the sample macro, a message box is displayed with the text “hello world!”. The icons in the tree view display a green arrow to show that a macro is executed.

 

Hello World!

 

Hinweis Note:
After a macro has been executed, the VBA environment is not automatically reset to design mode (for further information on the design mode, please refer to the VBA help). Therefore, the VBA environment can still process further events. Values written in variables remain available.

 

 

Available objects

By default, two objects are available in the VBA development environment:

 

CanEasyApplication
The CanEasyApplication object offers access to the functions of CanEasy provided via COM.

 

VBE
The VBE object offers access to the VBE interface, providing functions for external control of the development environment.

 

Further information on these topics can be found in the documentation of the COM interface and the Microsoft® VBA help.

 
References on CanEasy-VBA projects

 

It is possible to set references on other objects (like e.g. Active-X controls) via the VBA development environment. So it is also possible to set references to already existing VBA projects.

To do this, an existing VBA project must be selected via the reference list (VBA-IDE -> Tools -> References):
 

Reference on a VBA project within the workspace
Already within the workspace existing and saved VBA projects are show directly in the list of available references within the VBA development environment with their project names and could be selected.
Note: As path for projects stored within the workspace "?:\<projectname>" is displayed.
 

Reference on a exported VBA project
It is also possible to set a reference on an exported VBA project (*.vba). For this use button Browse... of the reference dialog to add the project to the list of possible references.
Note: As path the corresponding file path of the VBA project is displayed.