Developing Outside of the Box

David MeegoRecently I responded to a question on a newsgroup asking how to minimise a window from VBA (Visual Basic for Applications).

The short answer is that it is not possible.  But just because a Microsoft Dynamics GP window VBA object does not expose any methods or properties for the window state does not mean we have to give up.

Answering the post made me think about how we can overcome limitations by thinking outside of the box.  What other methods can we use to achieve the desired results?

In this case there were two possible solutions:

  • Sending the keystrokes with the VBA SendKeys command:

SendKeys "% n" ' Alt-Space, n

  • Using pass through sanScript to ask Dexterity to change the window state for you:

VBA Code Example for Customer Maintenance window

    ' ** Uncomment the line below to use early binding, requires
' ** reference to Dynamics Continuum Integration Library
' ** Early binding allows Intellisense to work on CompilerApp object
'Dim CompilerApp As New Dynamics.Application

' ** Comment out the line below to use early binding
Dim CompilerApp As Object

Dim CompilerMessage As String
Dim CompilerError As Integer
Dim CompilerCommand As String

    ' Create link without having reference marked
' ** Comment out the line below to use early binding
Set CompilerApp = CreateObject("Dynamics.Application")

CompilerCommand = ""
CompilerCommand = CompilerCommand & "Window_SetState(window RM_Customer_Maintenance of form RM_Customer_Maintenance, WINDOW_STATE_MINIMIZED);" & vbCrLf

    ' Execute SanScript
' ** Change Product ID to value from Dynamics.set for appropriate product
CompilerApp.CurrentProductID = 0 ' DYNAMICS

' ** Uncomment the line below if wanting to address fields added to modified window
'CompilerApp.CurrentProduct = CompilerApp.CurrentProduct & "!Modified"
CompilerError = CompilerApp.ExecuteSanscript(CompilerCommand, CompilerMessage)
If CompilerError <> 0 Then
MsgBox CompilerMessage
End If

    Set CompilerApp = Nothing


Note: The method of calling Dexterity sanScript from VBA using the Continuum Integration Library is not supported.

Another issue recently discussed on the blogs is the handling of modal dialogs. Dexterity does not have any triggers which can be registered against a modal dialog. This means that if you are attempting to drive the user interface from a customisation and a modal dialog opens your code is now stuck waiting for a user to dismiss the dialog.

Again there are methods that can be used to resolve this issue:

  • In the Integrating with Third Party Dictionary materials (available from here) a method of using an on-the-fly macro to control system dialogs is discussed. Here is some example code of running a macro to close a dialog.  The macro must be run before the dialog is opened.

Dexterity On-the-fly Macro Code Example

local integer l_file_id;
local long l_result;
local string macroname;
local boolean l_boolean;

{Create & Run Macro File}
pragma(disable warning LiteralStringUsed);

macroname = GetTempDir() of form coLetterWizard + "MBS_TEMP.MAC";
l_file_id = TextFile_Open(macroname, 0, 0);
l_result = TextFile_WriteLine(l_file_id, "NewActiveWin dictionary 'Microsoft Dynamics GP' form DiaLog window DiaLog ");
l_result = TextFile_WriteLine(l_file_id, " ClickHit field OK ");
l_boolean = TextFile_Close(l_file_id);

pragma(enable warning LiteralStringUsed);

run macro macroname;

{ Simulate Dialog opening }
warning "This is a System Dialog.";

{ Delete temporary macro file when no longer needed }
{ This code must execute after macro has closed dialog }
if File_Probe(macroname)then
end if;


Note: As macros run after the completion of background processes this method can have delay issues when there are background processes or reports being executed.

  • Another method, discussed by Sivakumar on his blog post is to use VBA to handle the dialog for us as VBA does have events for modal dialogs.

VBA Window_BeforeModalDialog Code Example

Private Sub Window_BeforeModalDialog(ByVal DlgType As DialogType, PromptString As String, Control1String As String, Control2String As String, Control3String As String, Answer As DialogCtrl)
Select Case PromptString
Case "Do you want to delete this record?"
Answer = dcButton1
Case Else
End Select
End Sub


Tip: Use a Breakpoint on the Select Case statement when you first write the code, then you can enter ? PromptString into the Immediate window (select View >> Immediate Window if it is not currently displayed) and then copy and paste the exact wording for the dialog into your code.

The reason for this post is to show how using other methods such as SendKeys, macros and hybrid code from more than one language can allow developers to get past limitations in the development environment. 

The take away message from this post is simple:

Don't be afraid to think Outside of the Box.


25-Sep-2009: Added copy and paste tip for modal dialog handling.

06-Jan-2012: Fix typo in VBA Window_BeforeModalDialog script.