VBA - Running VBA scripts after Login or before Logout Example

Sometimes you need to run VBA (Visual Basic for Applications) scripts after a successful login attempt.  Using the OK Button on the Company Selection window does not always work as the login could still fail. For example, the user is already logged into the selected company. Also this does not help capture the logging out.

In v7.50 of Great Plains and earlier you could add the Toolbar window to VBA and run scripts on the Window_AfterOpen() or Window_BeforeClose() events. From v8.00 onwards, the change in the menu navigation model means that the Toolbar window is no longer visible and so cannot be added to VBA.

Even though the Toolbar window cannot be added to VBA using the Tools >> Customize >> Add Current Window to Visual Basic menu option, you can import a blank Toolbar package to add the window and the main fields to VBA.

Below are the steps to create a generic Toolbar package which can be used to expose the current User, Company Name and User Date.  This information can be retrieved using the RetrieveGlobals.dll (v8.0), RetrieveGlobals9.dll (v9.0) or UserInfoGet object (v10.0). It can also be used to execute VBA code just after logging in, which is what we need it for.

1) Use Notepad.exe to create a text file on your Desktop called Toolbar.txt and copy the text below into it.

<Component Name="Toolbar" ProductId="0" Object="VBAForm" >
VBAForm "Toolbar"
{
Windows
{
Window "Main_Menu_1"
{
EventMode "0"
Fields
{
WindowField "(L) eEnterpriseUser ID"
{
ArrayIndex "0"
DisplayName "eEnterpriseUserID"
Local "true"
ProgrammerName "eEnterpriseUser ID"
}
WindowField "Company Name"
{
ArrayIndex "0"
DisplayName "CompanyName"
Local "false"
ProgrammerName "Company Name"
}
WindowField "Date"
{
ArrayIndex "0"
DisplayName "UserDate"
Local "false"
ProgrammerName "Date"
}
}
Title "Toolbar"
}
}
}
</Component>

2) Save the file and exit Notepad.exe. 

3) Rename the Toolbar.txt to Toolbar.package.

NOTE: To be able to rename the extension, you might need to display the file extensions if they are hidden. From a Windows Explorer window, use the menus to open the Folder Options window and on the View tab, uncheck "Hide extensions for known file types". Then click OK.

Now we can import the generic Toolbar package we created.

4) From Microsoft Dynamics GP menus click Tools, click Customize, click Customization Maintenance.

5) Click Import, click Browse and select Local. Find the Toolbar.package file and click open. Then click OK.

6) From the menu click Tools, click Customize, click Visual Basic Editor.

7) In the Visual Basic Editor, locate the Toolbar(Window) module under the Microsoft_Dynamics_GP project. Double click on the module and add the desired scripts. 

Now you know the steps, I have just opened the microwave and found a Toolbar.package I made earlier and have attached it to this post.

The package which works for v8.0, v9.0 & v10.0 is attached at the bottom of the article.

Toolbar.zip