How to use Word Mail Merge and Macros to Import Data

David MeegoThis post explains how to use Microsoft Office Word's Mail Merge functionality to Import Data into Microsoft Dynamics GP.  This method can be useful when working with master records or transactions not supported by any of the existing tools such as Integration Manager, eConnect or Web Services.

When attempting to import data into Microsoft Dynamics GP for a master record or transaction not supported by the usual tools, the usual option left is to perform a direct table import via the built-in Import Utility or using SQL Server tools such as Data Transformation Services (DTS) and Bulk Copy (BCP).  The problem with a direct table import is that it bypasses all the application business logic and that the resulting data created could not be 100% correct.  This could then cause issues later when using the application with the imported data.

The method described in this article uses Word's Mail Merge functionality to combine a recorded macro with a data set to create a new Macro file with will then enter the required data as though it was being entered by the user.

Note: This method works well when the entry of each record is identical.  If additional dialogs are displayed or different actions are required to import the transactions, the macro would not handle this.  It would be possible to use Word's IF conditional field functionality to make a macro which could handle these more complex situations, however that is beyond the scope of this article.

To demonstrate the method, this article will show how to import Sales Prospects into the Sales Prospect Maintenance window.  Below are the contents of the comma delimited source data text file:

Prospect ID, Customer Name, Contact Person, Address 1, Address 2, Address 3, City, State, Zip
PROSPECT 1, Name 1, Person 1, Address Line 1, Address Line 2, Address Line 3, City 1, State 1, 00001
PROSPECT 2, Name 2, Person 2, Address Line 1, Address Line 2, Address Line 3, City 2, State 2, 00002

The steps to import this data into Sales Prospect Maintenance are shown below:

STAGE A: Create Template Macro

  1. Launch Microsoft Dynamics GP and login.  You can use a Test company or the lesson company for this stage.

  2. Click Cards, click Sales and click Prospects to open the Sales Prospect Maintenance Window.

  3. Click on a field other than the primary key field (Prospect ID), for example, click on the Name field to take the focus away from the Prospect ID field.  This will ensure that the macro records the focus changing to the primary key field.

  4. Click Tools, click Macro, click Record and enter a macro file name to start recording the template macro.

  5. Click on the primary key field, for example Prospect ID and enter a dummy Prospect ID then press Tab. Use the name of the field where possible, PROSPECT ID.

  6. Enter dummy data into each of the fields that will be imported into and press Tab.  Where possible enter the name of the field into the field as this will make the creation of the mail merge document easier. For example: Name, Contact, Address, City, State, Zip.

  7. When all the fields have been populated with dummy data, click Save.

  8. Click Tools, click Macro, click Stop Record.

  9. Optional: To clean up, please select the just created record and delete it, it was only needed to create the macro file.

  10. Optional: Open the created Macro file with Notepad.exe. It should look similar to the following example:

    # DEXVERSION=XX.XX.XXXX.0 2 2
    CheckActiveWin dictionary 'default' form 'SOP_Prospect_Maintenance' window 'SOP_Prospect_Maintenance'
    NewActiveWin dictionary 'default' form 'SOP_Prospect_Maintenance' window 'SOP_Prospect_Maintenance'
    ActivateWindow dictionary 'default' form 'SOP_Prospect_Maintenance' window 'SOP_Prospect_Maintenance'
    MoveTo field 'Prospect ID'
    TypeTo field 'Prospect ID' , 'PROSPECT ID'
    MoveTo field 'Customer Name'
    TypeTo field 'Customer Name' , 'Name'
    MoveTo field 'Contact Person'
    TypeTo field 'Contact Person' , 'Contact'
    MoveTo field 'Address 1'
    TypeTo field 'Address 1' , 'Address Line 1'
    MoveTo field 'Address 2'
    TypeTo field 'Address 2' , 'Address Line 2'
    MoveTo field 'Address 3'
    TypeTo field 'Address 3' , 'Address Line 3'
    MoveTo field City
    TypeTo field City , 'City'
    MoveTo field State
    TypeTo field State , 'State'
    MoveTo field Zip
    TypeTo field Zip , 'Zip'
    MoveTo field 'Save Button'
    ClickHit field 'Save Button'

STAGE B: Create Word Mail Merge Document (based on Word 2007)

  1. Launch Microsoft Office Word.

  2. Click on the Office Button and click Open.

  3. Select the document type drop down list as All Files (*.*).

  4. Locate the Macro file created in the previous stage and click Open.

  5. Find the Macro command "CheckActiveWin" at the top of the list of Macro commands and change it to "ActivateWindow". This will ensure that the window is selected at the beginning of the data entry cycle.

  6. Click on the Mailings tab in the Office ribbon.

  7. Click Start Mail Merge and click Step by Step Mail Merge Wizard.

  8. Select the document type as Letters and click Next.

  9. Select the starting document as Use the current document and click Next.

  10. Select recipients as Use an existing list and click Browse and locate your data file.

  11. Check that Word has read your data file correctly on the Mail Merge Recipients window, click OK.

  12. Click Next to get to the Write your letter stage.

  13. Look for the TypeTo Macro commands and replace the text between the single quotes with merge fields. Use Insert Merge Field from the Office Ribbon.

    The resulting document should look like the following example:

    # DEXVERSION=XX.XX.XXXX.0 2 2
    ActivateWindow dictionary 'default' form 'SOP_Prospect_Maintenance' window 'SOP_Prospect_Maintenance'
    NewActiveWin dictionary 'default' form 'SOP_Prospect_Maintenance' window 'SOP_Prospect_Maintenance'
    ActivateWindow dictionary 'default' form 'SOP_Prospect_Maintenance' window 'SOP_Prospect_Maintenance'
    MoveTo field 'Prospect ID'
    TypeTo field 'Prospect ID' , '«Prospect_ID»'
    MoveTo field 'Customer Name'
    TypeTo field 'Customer Name' , '«Customer_Name»'
    MoveTo field 'Contact Person'
    TypeTo field 'Contact Person' , '«Contact_Person»'
    MoveTo field 'Address 1'
    TypeTo field 'Address 1' , '«Address_1»'
    MoveTo field 'Address 2'
    TypeTo field 'Address 2' , '«Address_2»'
    MoveTo field 'Address 3'
    TypeTo field 'Address 3' , '«Address_3»'
    MoveTo field City
    TypeTo field City , '«City»'
    MoveTo field State
    TypeTo field State , '«State»'
    MoveTo field Zip
    TypeTo field Zip , '«Zip»'
    MoveTo field 'Save Button'
    ClickHit field 'Save Button'

  14. Click Next on the Mail Merge Wizard to preview the data.

  15. Click Next on the Mail Merge Wizard to Complete the merge.

  16. Click Edit individual Letters ... to merge to a new document.

  17. Click the Office Button and click Save As.

  18. Select the Save as Type as Plain Text (*.txt).

  19. Enter the output file name and click save.

  20. On the File Conversion window, click OK to accept the default settings.

  21. Close Output document's window in Word.

  22. Select the Mail Merge Document based on the initial macro file.

  23. Click the Office Button and click Save As.

  24. Select the Save as Type as Word Document (*.docx) or Word 97-2003 Document (*.doc)

  25. Adjust the name if desired and click Save.

  26. Exit Word.

STAGE C: Running the Macro to Import the data

  1. Using File Explorer, Locate the Output text file from the previous stage.

  2. Rename the extension from *.txt to *.mac.

    Note:  You might need to uncheck the "Hide extensions for known file types" folder option to be able to rename the extension.

  3. Return to Microsoft Dynamics GP, if necessary change the company logged into.

  4. Click Cards, click Sales and click Prospects to open the Sales Prospect Maintenance Window.

  5. Click Tools, click Macro, click Play and enter now renamed output macro file name.

  6. The Macro will run and display a dialog when completed saying how long it took to run.

  7. Optional: Use the Lookup or Navigation buttons to view the newly imported records.

Please consult the online help documentation for Word if you have any questions about how the Mail Merge functionality works.

This is a technique that can be used when there is no eConnect or Integration Manager alternatives.

David

Ref: Internal KB 953437