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:

  11. # 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.

  14. 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’


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

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

  17. Click Edit individual Letters … to merge to a new document.

  18. Click the Office Button and click Save As.

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

  20. Enter the output file name and click save.

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

  22. Close Output document’s window in Word.

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

  24. Click the Office Button and click Save As.

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

  26. Adjust the name if desired and click Save.

  27. 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.

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


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

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

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

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

  8. 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

Comments (12)

  1. christina belding says:

    Exceptional post David-thanks!

    Do you find it helpful to enter more than 1 records worth of data when recording the macro…just to make sure it keeps going, then you take the second part to do the mail merge?

    Just curious.  Thanks for a great post!

    Chris

  2. David Musgrave says:

    Hi Chris

    While you could record the macro while entering a couple of records to confirm that the macro is the same, I would recommend that the final macro you use for your mail merge is a single record only.

    David

  3. tom says:

    Hi David:

    then other this method, Are there any other ways to complete the import data?

  4. David Musgrave says:

    Post from Jivtesh Singh at About Dynamics, Development and Life

    http://www.jivtesh.com/…/dynamics-gp-customizations-best.html

  5. Chuck says:

    This worked great except the account description only brought in the first word in my mail merge field (there are spaces & "-" in the descripton as we include the account name and department name).

  6. Nataraj S says:

    Its is an excellent precise post that help developers(dint have integration toolsknowledge) during integration.

  7. Tom says:

    Great stuff! Many thanks.

    Could this be used to enter stock count?

    1. Hi Tom

      Any repetitive task in GP could be automated with this technique.

      David