Example: Calling MINITAB from VBA

This Word template document demonstrates methods to automate data analysis and report generation using Microsoft Word VBA calls to MINITAB. The template file MUST be created/saved using File> Save As> Trusted Templates. The template is configured so that the embedded VBA code runs automatically whenever the template is used to create a new Word document.

If the Developer menu item is not visible in Word’s menu bar click Office Button> Word Options> Popular and check the check box Show Developer tab in the Ribbon.

To view the VBA code embedded in this document click Developer> Visual Basic. Then find and click Project> Modules> Module 1 in the Project Explorer window. Run the code by clicking the play () command button in the Visual Basic editor tool bar. VBA sends the following commands to MINITAB:

set c1

(1:5)10

end

random 50 c2;

normal 100 10.

print c1-c2

boxplot c2 * c1

Bookmarks were embedded in this Word document using Insert> Bookmark. Bookmarks are indicated with I-shaped symbols on the left side of the page. If they are not visible then go to and check Office Button> Word Options> Advanced> Show document content> Show bookmarks. The output from MINITAB’s print command is imported to the bookmark named MyDataTable located here below the table heading:

Table 1. Data from MINITAB printed at the MyDataTable bookmark.

The output from MINITAB’s boxplot command is saved to temporary file junk.jpg and then imported to a bookmark named MyBoxPlot here below the figure heading:

Figure 1. Boxplot from MINITAB printed at the MyBoxPlot bookmark.

Any other commands can be sent to MINITAB and their outputs imported into a Word document using similar methods. Rather than calling individual MINITAB commands from VBA it may be easier to call a MINITAB macro instead. The macro could also use ODBC to open a data source so that the Word document contains the analysis of the most recent data.

This template file is configured to automatically run your VBA code when it is used to create a new document (using File> New> Templates> Create). This function is provided by the following subroutine that was added to Visual Basic Editor> Projects> Microsoft Word Objects> ThisDocument:

Private Sub Document_New()

Call Module1. Call_MINITAB_from_VBA

End Sub

Automatically running the code like this is especially useful if the code is also importing data from a constantly-updated data source using ODBC.