Error only being caught on the first instance in a for loop? VBA

Error only being caught on the first instance in a for loop? VBA

Anonymous
Not applicable
1,553 Views
3 Replies
Message 1 of 4

Error only being caught on the first instance in a for loop? VBA

Anonymous
Not applicable

I'm having some trouble with an error that is occurring in part of my code. I don't know a great deal about error handling but I can't see why what I have done isn't working.

 

I'm using a VBA macro to find which faces have threads on, it works for all the threaded faces but produces an error if there isn't a thread there. This is fine as long as the error can be dealt with and doesn't stop the macro.

 

Here's the main part of the code:

'oAlignedFaceColl is a collection of cylindrical faces
   For i = 1 To oAlignedFaceColl.Count

Dim oThreadInfos As ObjectCollection Set oThreadInfos = oAlignedFaceColl(i).ThreadInfos On Error GoTo NextStep Set oThreadInfo = oAlignedFaceColl(i).ThreadInfos(1) Debug.Print Err.Number, "error number" NextStep: Next

The first error that's found does go to the line NextStep however the second time

0 Likes
1,554 Views
3 Replies
Replies (3)
Message 2 of 4

MechMachineMan
Advisor
Advisor

You have half a post; seems to have been cut off.

 

However, it may be an error because you don't handle the error; there should be a resume statement somewhere, or a Err.Clear somewhere (I think). This alternative code below should also work.

 

'oAlignedFaceColl is a collection of cylindrical faces
   For i = 1 To oAlignedFaceColl.Count

	Dim oThreadInfos As ObjectCollection
        Set oThreadInfos = oAlignedFaceColl(i).ThreadInfos

        On Error Resume Next
            Set oThreadInfo = oAlignedFaceColl(i).ThreadInfos(1)  
  Next     

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 3 of 4

Anonymous
Not applicable

Apologies for that, I didn't realise only half was posted! Unfortunately I need the goto ( I skipped an unimportant chuck of code). I also tried using an err.clear and on error resume next in varying places with no success.

I have since found an alternative way of doing what I want but am still curious for the reason behind it not originally working if you have any other suggestions?

0 Likes
Message 4 of 4

ruthsteed
Autodesk
Autodesk

In the "error handling" section (at the label "NextStep:" you need to have a "Resume" statement to return to the loop body correctly. This can be either a simple "Resume Next" if the code can reasonably continue after the error, or a "Resume AfterErrorLabel" where "AfterErrorLabel" is another location in your code.

 

For example, to just return to the Debug.Print (which is unaffected by the error line before):

   'oAlignedFaceColl is a collection of cylindrical faces

   For i = 1 To oAlignedFaceColl.count

 

       Dim oThreadInfos As ObjectCollection

       Set oThreadInfos = oAlignedFaceColl(i).ThreadInfos

       On Error GoTo NextStep

       Set oThreadInfo = oAlignedFaceColl(i).ThreadInfos

       Debug.Print Err.Number, "error number"

   Next

NextStep:

       Resume Next

' …

 

You can find more information on this by Googling "vba error handling in loop", but it's probably easier to not bother with the error handling at all in this case. The real problem is that oAlignedFaceColl(i).ThreadInfos is Nothing. When you try to access anything inside it, it fails. It would be simpler to just check that oThreadInfos is set.

 

Something like this, doesn't require any additional On Error directives:

   For i = 1 To oAlignedFaceColl.count

       Dim oThreadInfos As ObjectCollection

       Set oThreadInfos = oAlignedFaceColl(i).ThreadInfos

       If Not oThreadInfos Is Nothing Then

           Debug.Print oThreadInfos.count; " threadinfos"

           If oThreadInfos.count > 0 Then

               Debug.Print oThreadInfos(1).CustomThreadDesignation

           End If

       Else

           Debug.Print "No threadinfos"

       End If

   Next



Ruth Steed
Inventor Development Team
Autodesk, Inc.