Help with array

Help with array

Anonymous
Not applicable
1,744 Views
6 Replies
Message 1 of 7

Help with array

Anonymous
Not applicable

Hi,

 

I'm pretty new to VBA and I'm trying to understand how arrays work so I that can use one in a macro for manipulating a Parts List.

 

My test code uses an array called AAARowList. When I try to assign a value to the first element, I get "Compile error: Invalid use of property". I'm sure that I'm overlooking something simple, but I've never used an array before, so I'm clueless. I haven't been able to find many examples of dynamic arrays used in a similar situation either. Here's my code:

 

Public Sub Test()
    Dim oDrawDoc As DrawingDocument
    Set oDrawDoc = ThisApplication.ActiveDocument
    Dim oPartList As PartsList
    Set oPartList = oDrawDoc.SelectSet.Item(1)
    Dim MyRow As PartsListRow
    Dim rowIndex As Integer
    Set MyRow = oPartList.PartsListRows.Item(rowIndex)
    Dim AAARowList(0) As PartsListRow
    Dim AAARowIndex As Integer
    rowIndex = 0
    AAARowIndex = 0
       
    AAARowList(AAARowIndex) = MyRow
    AAARowIndex = AAARowIndex + 1
    ReDim Preserve AAARowList(AAARowIndex + 1)
 
End Sub

 

Eventually, the test code will be used in a bigger macro that cycles through the rows in a parts list. Any row that meets a certain criteria will be saved in the array so that action can be performed later on in the macro.

 

I appreciate any help!

 

Nate

 

0 Likes
Accepted solutions (1)
1,745 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable

Maybe this link'll help?  Office VBA syntax is identical to Inventor's VBA.

http://www.anthony-vba.kefra.com/vba/vbabasic3.htm

 

One thing to keep in mind with Inventor's collections is that their indexes start with 1.  So if you're looping through an array that corresponds to an Inventor collection, make sure to add one to the index.

 

Edit: Also this link:

http://msdn.microsoft.com/en-us/library/aa164778%28office.10%29.aspx

0 Likes
Message 3 of 7

Anonymous
Not applicable

I don't think this question belongs on this forum, but anyway, I see one error imediately...

 

Dim rowIndex As Integer
Set MyRow = oPartList.PartsListRows.Item(rowIndex)

 

Here you have declared rowIndex, but not set the value. The value will then (probably) be 0

oPartList.PartsListRows.Item(0) does not exist, so this will fail.

Try to change the line to 

Dim rowIndex As Integer=1

 

The rest of the code is probably failing for the same reason

 

R

 

 

0 Likes
Message 4 of 7

Anonymous
Not applicable

The links look very helpful, however, I'm not finding out what I need from them. I think I've tried to follow the examples given, but my code is still not working.

 

Inventor's collection indexes may indeed start at 1, but I'm noticing from examples that array indexes normally start at 0. Anyway, I tried 1 and 0 and neither worked.

 

Thanks for the help though. I'm sure the those links will help me out tremendously with other tasks

 


peter.townsend wrote:

Maybe this link'll help?  Office VBA syntax is identical to Inventor's VBA.

http://www.anthony-vba.kefra.com/vba/vbabasic3.htm

 

One thing to keep in mind with Inventor's collections is that their indexes start with 1.  So if you're looping through an array that corresponds to an Inventor collection, make sure to add one to the index.

 

Edit: Also this link:

http://msdn.microsoft.com/en-us/library/aa164778%28office.10%29.aspx


 

0 Likes
Message 5 of 7

Anonymous
Not applicable

Why wouldn't this question belong on this forum? I'm trying to customize an Inventor Parts List. This is the Inventor Customization discussion group right? What other forum would you suggest that I use instead?

 

In regards to your suggestion with my code, I do see that I declared the index farther down in the code. So I moved it up as shown below, but it stills fails.

 

Public Sub Test()
    Dim oDrawDoc As DrawingDocument
    Set oDrawDoc = ThisApplication.ActiveDocument
    Dim oPartList As PartsList
    Set oPartList = oDrawDoc.SelectSet.Item(1)
    Dim rowIndex As Integer
    rowIndex = 0

 

    Dim MyRow As PartsListRow
    Set MyRow = oPartList.PartsListRows.Item(rowIndex)
    Dim AAARowList(0) As PartsListRow
    Dim AAARowIndex As Integer
    rowIndex = 0
    AAARowIndex = 0
       
    AAARowList(AAARowIndex) = MyRow
    AAARowIndex = AAARowIndex + 1
    ReDim Preserve AAARowList(AAARowIndex + 1)
 
End Sub

 

Any other suggestions?

 


@Anonymous wrote:

I don't think this question belongs on this forum, but anyway, I see one error imediately...

 

Dim rowIndex As Integer
Set MyRow = oPartList.PartsListRows.Item(rowIndex)

 

Here you have declared rowIndex, but not set the value. The value will then (probably) be 0

oPartList.PartsListRows.Item(0) does not exist, so this will fail.

Try to change the line to 

Dim rowIndex As Integer=1

 

The rest of the code is probably failing for the same reason

 

R

 

 


 

0 Likes
Message 6 of 7

Anonymous
Not applicable

Well, perhaps it belongs in here... You are getting answers 🙂

 

I actually sat down and tested your code in Inventor this time...

 

Here are my suggestion (and it runs :-))

 

Public Sub Test()    

  Dim oDrawDoc As DrawingDocument    

  Set oDrawDoc = ThisApplication.ActiveDocument    

  Dim oPartList As PartsList    

  Set oPartList = oDrawDoc.SelectSet.Item(1)  

  Dim rowIndex As Integer    

  rowIndex = 1     

  Dim MyRow As PartsListRow    

  Dim PLCount As Integer    

  PLCount = oPartList.PartsListRows.Count

  'rowIndex MUST be 1, otherwise it fails, as I suggested in my previous post. (You can try it if you don't believe me :-))    

  Set MyRow = oPartList.PartsListRows.Item(rowIndex)    

  Dim AAARowList() As PartsListRow

  'Could not use Dim AAARowList(PLCount - 1) As PartsListRow, so I imediately Redim below...

  ReDim AAARowList(PLCount - 1)    

  Dim AAARowIndex As Integer

'Here you can use index 0, this is a "normal" array

AAARowIndex = 0    

Dim plRow As PartsListRow    

For Each plRow In oPartList.PartsListRows

'Here you have forgotten Set        

Set AAARowList(AAARowIndex) = MyRow        

AAARowIndex = AAARowIndex + 1    

Next        

'For some reason this didnt work, see my workaround further up

'ReDim Preserve AAARowList(AAARowIndex + 1) 

End Sub

 

You are probably doing this for some good reason, but in the end you have an array of PartsListRows (AAARowList).

This is actually the same as oPartList.PartsListRows

 

And I guess I don't need to say that you have to select a partslist before you run this macro 🙂

R

It's been a while since I used VBA/VB6... don't miss it at all 🙂

0 Likes
Message 7 of 7

Anonymous
Not applicable
Accepted solution

R,

 

Thanks for your help. As you pointed out, I left "Set" out of one line of code which caused the initial error. Then using the ReDim immediately after the dynamic array was declared, cleared up the other error.

 

Thanks a million!

0 Likes