Showing results for 
Search instead for 
Did you mean: 

Need help debugging my code...

Valued Contributor
Valued Contributor

With the help others I have been trying  to develop a a very robust journal. It has a decent amount of code in it and it is starting to get hard to manage. I am trying to incorporate a block of code from another journal that I found online and I am getting runtime errors. I can't figure out how to remedy the problem.


My journal was written to write Mass properties data to an excel sheet. Specifically volume, enters and extents for each component in an assembly. I wanted to add some code to group the components within excel so the structure is easier to manage.


I will include both journals in the post.


 The journal I am trying to develop is called "MeasureBodies". I added code from the "BOM to Excel" Journal into the end of Sub Main() In my journal. Specifically what I added was this:


          Dim objWorksheet As Object
          Dim rngStart As Object
          Dim rngEnd As Object
          Dim intIndent As Single
          Dim intLeft As Single
          Dim intTopRow As Integer
          Dim i As Integer
          Dim j As Integer
          Dim lngStart As Long
          Dim lngLevel As Long
          intIndent = 6.75
         Dim colLevel As Integer = 1
         Const xlDown As Long = -4121
         Const xlAbove As Long = 0
         Const xlLeft As Long = -4131
         Dim lngLastRow As Long = objWorksheet.Cells(2, colLevel).End(xlDown).Row

         '####### Add groupings to excel structure ################
        With objWorksheet.Outline
            .AutomaticStyles = False
            .SummaryRow = xlAbove
            .SummaryColumn = xlLeft
        End With
        For j = 8 To 1 Step -1
            lngStart = 0
            'Loop through rows
            For i = 2 To lngLastRow
                lngLevel = Int(objWorksheet.Cells(i, colLevel).Value)
                If lngLevel = j And lngStart = 0 Then
                    lngStart = i
                ElseIf lngLevel < j And lngStart > 0 Then
                    objWorksheet.Rows(lngStart & ":" & i - 1).EntireRow.Group()
                    lngStart = 0
                ElseIf i = lngLastRow And lngStart > 0 Then
                    objWorksheet.Rows(lngStart & ":" & i).EntireRow.Group()
                    lngStart = 0
                End If
            Next i
        Next j
        '####### End Add groupings to excel structure ################



I add it to the end of Sub Main because the point of the code is to add groupings after all the component information is written to Excel.


I tried adding code to the end of the


"For Each temp As asmcontainer In theAsm.AsmComponents" loop.


So it would group the data on the fly but  that isn't working the way I want it to.


I want it to group each parent with there children underneath them, the same way it does in the "BOM to EXCEL" Journal.  So I tried integrating the code from that journal to see what the results might be.


When I try to run my MeasureBodies journal as it is, it produces an error:


"System.ArgumentNullException: Value cannot be null"


It points to line 147.

which is:


"For i = 2 To lngLastRow "  


 My guess is the variable "lngLastRow" needs to be set to the last row number that was written to in Excel. I'd guess this wasn't set or the value is incorrect causing the code to overshoot into empty cells. The empty cell value can't be converted to an integer and I get the error.
However I am not sure if this is true or not. Even if it was I am not sure what code I would use
to accomplish this.
I am running TeamCenter with NX
If anybody has any ideas on how to better approach this please let me know.
Any help with this would be greatly appreciated!





Re: Need help debugging my code...

Honored Contributor
Honored Contributor

You have written text into column one (the part name) and later try to read it and convert it to an integer. The part name cannot be converted to an integer and you get an error.

Re: Need help debugging my code...

Valued Contributor
Valued Contributor

Oh okay, so how could I change it so it will actually group the components in Excel? Does it need to pick up an empty column, or one with a numbers in it?