Search code examples
vbaexcelminitab

How do I Launch Existing Minitab Project using VBA and interact with it?


I have much larger plans for my VBA-Minitab interaction, but I need to start small first. I am simply attempting to open an existing Minitab Project, count the existing sheets, and display a message box with the count. I have searched for answers in the 15 posts about Minitab as well as in the documentation but I have been unable to find the solution.

            Sub GetModel()

            Dim MtbApp As Mtb.Application
            Dim MtbProj As Mtb.Project
            Dim MtbSheets As Mtb.Worksheets

            Set MtbApp = New Mtb.Application
            Set MtbProj = MtbApp.ActiveProject
            Set MtbSheets = MtbProj.Worksheets

            MtbApp.UserInterface.Visible = False
            MtbApp.UserInterface.DisplayAlerts = False

            MtbApp.Open "P:\Tools\Models\MTB Model Test\NSS_Model_Test.MPJ"

            MsgBox "There are " & MtbSheets.Count & " worksheets in the project."

            MtbApp.Quit
            End Sub

When I run the code above, the appropriate project opens, a message box opens displaying that 1 sheet is in the project, and the project closes. This particular project has 8 sheets in it. Also, due to the research I did earlier I decided to check the task manager to see if the minitab application was actually closing. It appears to continue running in the background using system resources even though the code prompts it to quit.


Solution

  • This specific example was errored due to the order with which I initialized the variables. The code below functions as expected.

            Sub GetModel()
    
            Dim MtbApp As Mtb.Application
            Dim MtbProj As Mtb.Project
            Dim MtbSheets As Mtb.Worksheets
    
            Set MtbApp = New Mtb.Application
    
            MtbApp.UserInterface.Visible = False
            MtbApp.UserInterface.DisplayAlerts = False
    
            MtbApp.Open "P:\Tools\Models\MTB Model Test\NSS_Model_Test.MPJ"
    
            Set MtbProj = MtbApp.ActiveProject
            Set MtbSheets = MtbProj.Worksheets
    
    
            MsgBox "There are " & MtbSheets.Count & " worksheets in the project."
    
            MtbApp.Quit
            End Sub