Jaa


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
 File_Delete(macroname);
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.

David

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

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

Comments

  • Anonymous
    April 23, 2009
    PingBack from http://blogs.msdn.com/developingfordynamicsgp/archive/2008/08/11/choosing-a-development-tool.aspx

  • Anonymous
    September 25, 2009
    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. Malek.

  • Anonymous
    September 29, 2009
    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. David

  • Anonymous
    September 29, 2009
    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. Regards. Malek

  • Anonymous
    May 27, 2010
    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?

  • Anonymous
    May 27, 2010
    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. David

  • Anonymous
    July 16, 2010
    Hi David, in relation to the modal dialog, how do I catch it and determine the answer using the VS toolkit?

  • Anonymous
    July 16, 2010
    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. David

  • Anonymous
    July 18, 2012
    Posting from Aaron Berquist www.aaronberquist.com/.../creating-custom-desktop-alerts-in-dynamics-gp