6 Replies Latest reply: May 15, 2014 9:54 AM by Nick Osdale-Popa RSS

    VB and Monarch

    Stephen Reid

      Hi all,

       

      I use the following lines of code to automatically set text and model files to print the summaries.

       

      [font="courier"]    Set Monarchobj = CreateObject("Monarch32")

           

          openfile = Monarchobj.setreportfile("S:steve_rmonarchReportsFimsctfim.txt", False)

          If openfile = False Then

          End If

           

          openfile = Monarchobj.setmodelfile("S:steve_rmonarchModelsFimsctfim2.mod")

          If openfile = True Then

              SummaryCount = Monarchobj.SummaryCount

              For LoopCounter = 0 To (SummaryCount - 1) Step 1

                  Monarchobj.CurrentSummary = Monarchobj.GetSummaryNameAt(LoopCounter)

                  Monarchobj.printsummary (True)

              Next LoopCounter

          Else

          End If

          Monarchobj.closealldocuments

          Monarchobj.Exit

          Set Monarchobj = Nothing[/font][/quote]I know you can pass variables into the set file functions but does anyone know if you can pass an array of strings into them.  The reason for this is I have 10 different files which need to be printed together and at the moment I just repeat the above code 10 times.  If I can pass an array then at least I can just loop through each location of the array.

       

      Any suggestions welcome

       

      Thanks

      Stephen Reid

        • VB and Monarch
          Stephen Reid

          Hi again,

           

          I had a bit of spare time on my hands and had a little play with this today.  I came up with the following code:

           

          [font="courier"]    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

           

                  Dim Monarchobj = CreateObject("Monarch32")         'Create the object

                  Dim openfile

                  Dim summaryCount, loopCounter, arrayCounter As Integer

                  Dim arrayReports() As String = {"ctfim.txt", "ctfim.txt", "ivfim.txt", "ivfim.txt", "pifim.txt", "pocofim.txt", "sofim.txt", "wofim.txt", "wofim.txt"}

                  Dim arrayModels() As String = {"ctfim.mod", "ctfim2.mod", "ivfim.mod", "ivfim2.mod", "pifim.mod", "pocofim.mod", "sofim.mod", "wofim.mod", "wofim2.mod"}

                  Dim reportPath As String = "S:steve_rmonarchReportsFims"

                  Dim modelPath As String = "S:steve_rmonarchModelsFims"

           

                  arrayCounter = 0

           

                  Do While arrayCounter <= 8

           

                      openfile = Monarchobj.setreportfile(reportPath & arrayReports(arrayCounter), False) 'Open the report file

           

                      openfile = Monarchobj.setmodelfile(modelPath & arrayModels(arrayCounter))  ' Open the model file

                      If openfile = True Then

                          summaryCount = Monarchobj.SummaryCount                 ' Determine the # of summaries

                          For loopCounter = 0 To (summaryCount - 1) Step 1

                              Monarchobj.CurrentSummary = Monarchobj.GetSummaryNameAt(loopCounter)         'Apply the Summary

                              Monarchobj.printsummary(True)

                          Next loopCounter

                      Else

                      End If

                      Monarchobj.closealldocuments()

                      Monarchobj.Exit()

                      Monarchobj = Nothing

                      arrayCounter = arrayCounter + 1

                  Loop

           

              End Sub[/font][/quote]This works for the first report and model and prints out the summaries, however on the second iteration of the loop it breaks out at openfile = Monarchobj.setreportfile.  The error I get is

           

          An unhandled exception of type 'System.NullReferenceException' occurred in microsoft.visualbasic.dll

           

          Additional information: Object variable or With block variable not set.

          /quoteDoes anyone have any ideas?

           

          Thanks again

          Stephen Reid

          • VB and Monarch
            Stephen Reid

            Sorry,

             

            I think I'm cracking up!! I have now taken the following code out.

             

            [font="courier"]            

            Monarchobj.closealldocuments()

            Monarchobj.Exit()

            Monarchobj = Nothing[/font][/quote]Unfortunately this doesn't work either.  It runs through the first 3 reports and models and then gets a Monarch.exe exception error when the 4th model tries to open.  I also get the follwoing error from VB:

             

            An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in microsoft.visualbasic.dll

             

            Additional information: The server threw an exception.

            /quoteOnce again, can anyone help me?

             

            Stephen Reid

            • VB and Monarch
              Nick Osdale-Popa

              I've come up with an Excel97 (it can be used with Access with slight modifications) Class Model that helped me load in several files to one model.

               

              I'm quite prode of the code (though it's not well documented), so I'm not eager to post it here.  Not sure if I can call it my own since it is sort of an extension to what Datawatch provides in its Programmer's Guide. (What do you think, Datawatch, can I call the code proprietary?)

               

              Um..um..hmmm..Send me a PM with your email and I'll send what I use. (I'm really reluctant to send it because I've worked so hard on it)  :rolleyes:

              • VB and Monarch
                Gareth Horton

                Nick

                 

                You can certainly put "Optimized by Nick", but we wouldn't want to lose the right to distribute the original code ourselves 

                 

                Gareth

                 

                Originally posted by Nick Osdale-Popa:

                I've come up with an Excel97 (it can be used with Access with slight modifications) Class Model that helped me load in several files to one model.

                 

                I'm quite prode of the code (though it's not well documented), so I'm not eager to post it here.  Not sure if I can call it my own since it is sort of an extension to what Datawatch provides in its Programmer's Guide. (What do you think, Datawatch, can I call the code proprietary?)

                 

                Um..um..hmmm..Send me a PM with your email and I'll send what I use. (I'm really reluctant to send it because I've worked so hard on it)   :rolleyes: /b[/quote]

                • VB and Monarch
                  Gareth Horton

                  Stephen,

                   

                  Take a look at this.

                   

                  Note I have changed a couple of things - the main problem was that you should have been instantiating and destroying the monarch object within the loop.

                   

                  You were creating the object outside the loop, then destroying it in the loop, hence there was no object available on the second iteration.

                   

                  Gareth

                   

                  [font="courier"]  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

                   

                   

                          Dim Monarchobj

                          Dim openfile As Boolean

                          Dim openmod As Boolean

                          Dim summaryCount, loopCounter, arrayCounter As Integer

                          Dim arrayReports() As String = {"classic.prn", "classic.prn", "classic.prn", "classic.prn", "classic.prn", "classic.prn", "classic.prn", "classic.prn", "classic.prn"}

                          Dim arrayModels() As String = {"lesson12.mod", "lesson12.mod", "lesson12.mod", "lesson12.mod", "lesson12.mod", "lesson12.mod", "lesson12.mod", "lesson12.mod", "lesson12.mod"}

                          Dim reportPath As String = "c:program filesmonarch eports"

                          Dim modelPath As String = "c:program filesmonarchmodels"

                          Dim currentreport As String

                          Dim currentmodel As String

                   

                          arrayCounter = 0

                   

                          Do While arrayCounter <= 8

                   

                              Monarchobj = CreateObject("Monarch32", "")

                   

                              currentreport = reportPath & (arrayReports(arrayCounter))

                              currentmodel = modelPath & (arrayModels(arrayCounter))

                   

                              openfile = Monarchobj.setreportfile(currentreport, False) 'Open the report file           

                              openmod = Monarchobj.setmodelfile(currentmodel) ' Open the model file  

                   

                   

                              If openmod = True Then

                                  summaryCount = Monarchobj.SummaryCount ' Determine the # of summaries               

                   

                                  For loopCounter = 0 To (summaryCount - 1) Step 1

                                      Monarchobj.CurrentSummary = Monarchobj.GetSummaryNameAt(loopCounter)         'Apply the Summary                   

                                      'Monarchobj.printsummary(True)

                                  Next loopCounter

                              Else

                              End If

                   

                              arrayCounter = arrayCounter + 1

                   

                              Monarchobj.closealldocuments()

                   

                              Monarchobj.Exit()

                   

                              Monarchobj = Nothing

                   

                   

                          Loop

                      End Sub /font[/quote][quote]Originally posted by Stephen Reid:

                  Hi again,

                   

                  I had a bit of spare time on my hands and had a little play with this today.  I came up with the following code:

                   

                  [font="courier"]    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

                   

                          Dim Monarchobj = CreateObject("Monarch32")         'Create the object

                          Dim openfile

                          Dim summaryCount, loopCounter, arrayCounter As Integer

                          Dim arrayReports() As String = {"ctfim.txt", "ctfim.txt", "ivfim.txt", "ivfim.txt", "pifim.txt", "pocofim.txt", "sofim.txt", "wofim.txt", "wofim.txt"}

                          Dim arrayModels() As String = {"ctfim.mod", "ctfim2.mod", "ivfim.mod", "ivfim2.mod", "pifim.mod", "pocofim.mod", "sofim.mod", "wofim.mod", "wofim2.mod"}

                          Dim reportPath As String = "S:steve_rmonarchReportsFims"

                          Dim modelPath As String = "S:steve_rmonarchModelsFims"

                   

                          arrayCounter = 0

                   

                          Do While arrayCounter <= 8

                   

                              openfile = Monarchobj.setreportfile(reportPath & arrayReports(arrayCounter), False) 'Open the report file

                   

                              openfile = Monarchobj.setmodelfile(modelPath & arrayModels(arrayCounter))  ' Open the model file

                              If openfile = True Then

                                  summaryCount = Monarchobj.SummaryCount                 ' Determine the # of summaries

                                  For loopCounter = 0 To (summaryCount - 1) Step 1

                                      Monarchobj.CurrentSummary = Monarchobj.GetSummaryNameAt(loopCounter)         'Apply the Summary

                                      Monarchobj.printsummary(True)

                                  Next loopCounter

                              Else

                              End If

                              Monarchobj.closealldocuments()

                              Monarchobj.Exit()

                              Monarchobj = Nothing

                              arrayCounter = arrayCounter + 1

                          Loop

                   

                      End Sub[/font][/quote]This works for the first report and model and prints out the summaries, however on the second iteration of the loop it breaks out at openfile = Monarchobj.setreportfile.  The error I get is

                   

                    /size[quote]quote:[/size]

                  An unhandled exception of type 'System.NullReferenceException' occurred in microsoft.visualbasic.dll

                   

                  Additional information: Object variable or With block variable not set.

                  /quoteDoes anyone have any ideas?

                   

                  Thanks again

                  Stephen Reid[/b][/size][/QUOTE]

                  • VB and Monarch
                    Nick Osdale-Popa

                    Not sure if you'll run into memory errors (lack of resources) that way or not. May want to create the object before the loop and then destroy it afterwards, that way only one object is being used.