Developing Outside of the Box

David Meego - Click for blog homepageThis is a reposting of an article I originally wrote on my Developing for Dynamics GP blog.

Recently 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;</span>

{Create &amp; 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 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.

This article was originally posted on the Developing for Dynamics GP Blog and has been reposted on

11 thoughts on “Developing Outside of the Box

  1. Thanks David for giving us the idea of how to get some of VBA capabilities to Dex.
    However, I have a request: Could you please place the "Printable Version" button in the blogs pages so we can save the material easily, similar to Partner Source pages?
    Thanks again.


  2. Hi Malek
    I will pass your feedback on about a Printable Version. I have always found that printing from the blog works fine as it is.


  3. You’re right, but Printable version allows me to save the pages as .mht files. (Helpful when I need to search for a subject (words) in my computer). Currently I’m copying the text area and paste it in FrontPage.


  4. Is it possible to create windows on the Fly. i.e. using code.
    I know this is not possible in Dexterity or VBA do any of the other tools allow you to create objects using code at runtime?


  5. Hi Spalterpea
    Dexterity, VBA and Visual Studio Tools don't have the ability to create Forms, windows or controls on the fly.
    However, some clever development can fake this.  You can have generic windows with generic fields and just hide, show and move the fields as required.  This is exactly how the Extender product "creates" windows on the fly.


  6. Hi Jasmine
    Short answer: You can't.  
    Long answer: You could use some VBA and store the answer in the DUOS and then use VSTools to access the SY_User_Object_Store to read the value.


Please post feedback or comments

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.