sábado, 17 de outubro de 2015

Explaining the LOG_MODULE - Reading 01

This is the first module we are publishing, and we haven't still explained this "module" approach - we'll get on that. So although we will explain, soon, how to import and export a VBA module, just for this time, we will assume that the reader knows how to do it and we'll go to the most basic module we develop - the LOG_MODULE.

The LOG_MODULE is, by far, the most used log in our vba programming method. All the modules we developed use this module, so it is very important that that reader be comfortable with it.

The LOG_MODULE, although very important, is actually very simple (by the way, simplicity is the core idea of this blog). The module contains functions that abstract the log printing. So, once the user have imported the log module, he or she will be able to print application log with a single line of code - all the abstraction is made under the curtains so that logging become a straightforward and simple operation.

The reason this mode exists is in line with the idea that we treat VBA applications with the same seriousness that we would treat a, say, JAVA or C++ application. We believe applications must print log and this module help us to do that in a simple fashion.

So, before we go into the details of the LOG_MODULE, let's create a routine that uses it. To do so, follow the following steps:

1. Open a Excel 2013 workbook.
2. Access the VBA Editor (Press ALT+F11)
3. Go to the toolbar, click "Insert", then click "Module".
4. If the "Project Explorer Window is not visible, click "View" then click "Project Explorer"
5. In the Project Explorer Window, find the module you have just created (usually named "Module 1") and change its name to "MAIN" using the "Properties Window" (if it not visible, press "F4"). This step is not really necessary, but it is our 1st good practice - to name the modules.
6. Import the LOG_MODULE (you can both import the MODULE_MODULE.bas file or to create a new module, name it to "LOG_MODULE" and then to paste the "LOG_MODULE" code on the editor - the code is available in another post)
6. Open the "MAIN" module editor and paste the code below (codes are pasted in italic).

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' @module MAIN_MODULE: All macros and functions that are called from the worksheets should be placed in here
' @author Yan França Tosta
' @version 37
' @since 08/2015
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

' This is an example routine
Public Sub MAIN_Hello_World_With_Log()
    
    LOG_MODULE.Log "Starting the MAIN_Hello_World sub"
    
    MsgBox "Hello World", vbInformation, "Hello World"
    
    LOG_MODULE.Log "The MAIN_Hello_World sub has finished"
    LOG_MODULE.Log "main", 0
    Exit Sub
    
End Sub

7. Click "Debug", then click "Compile VBA Project".
8. Save the workbook with a name, say, "log_module_test.xlsm" as a "macro-enabled" workbook.
9. Click "Run" then click "Run Sub/User Form" (or press F5').

A message box with the message "Hello World" should appear. Press "OK" to finish the routine.

Now open the Windows Explorer and go to the "C:\TEMP\" folder. There you will find a ".log" file named "log_module_test_[DATE].log', where [DATE] is the current date. Open this file with a text editor and you will read something like this:

2015-10-17 18:19 - Starting the MAIN_Hello_World sub
2015-10-17 18:20 - The MAIN_Hello_World sub has finished
2015-10-17 18:20 - main - EXIT SUCCESS

This is the log message. And the file you've just opened is the log file.

-------------------------------------------
DOWNLOAD:

File Name: LOG_MODULE.bas

Link for download: https://www.dropbox.com/s/etdqd76qcgeg8vb/LOG_MODULE_EXAMPLE.xlsm?raw=1

Nenhum comentário:

Postar um comentário