Pls somebody can helps me with this issue ?
I got an automation error in a VBA routine thath manage the files (open a flile search with selectioset an objetc, modify, save and close for underd files).
Attached the routrine, for your info, but seems the error hang the routine in random point without a precise instruction.
Thank you
Private Function LineCount(FileName As String) As Long i = 0 Open FileName For Input As #1 Do While Not EOF(1) i = i + 1 Line Input #1, Dado Loop Close #1 MyLineCount = i End Function Sub CambiaNome() MyPath = "D:\111111 - ZZZZ_New\ID11STD3604_rev1\" Dim Ssnew As Object Dim Entity As Object Call LineCount(MyPath & "DWG_NEW.txt") ReDim Preserve SourceDWG(MyLineCount) ReDim Preserve DestinationDWG(MyLineCount) MyDestinationPath = MyPath & "DWG_NEW.txt" Open MyDestinationPath For Input As #1 X = 0 Do While Not EOF(1) ' Loop until end of file. Input #1, SourceDWG(X) ' Read data into two variables. Input #1, DestinationDWG(X) ' Read data into two variables. X = X + 1 Loop Close #1 'IntGroupCode(0) = -4 'VarGroupValue(0) = "<OR" IntGroupCode(0) = 0 VarGroupValue(0) = "INSERT" IntGroupCode(1) = 2 VarGroupValue(1) = "NUMFG,NUMFGS" 'IntGroupCode(3) = 0 'VarGroupValue(3) = "TEXT" 'IntGroupCode(4) = 0 'VarGroupValue(4) = "MTEXT" 'IntGroupCode(5) = "-4" 'VarGroupValue(5) = "OR>" 'intGroupCode(0) = 0 'varGroupValue(0) = "INSERT" 'intGroupCode(1) = 2 'varGroupValue(1) = "NEW_SAIPEM" Dim MyNewPath As String For i = 0 To MyLineCount - 1 For Each Pippo In ThisDrawing.SelectionSets If Pippo.Name = "BOM1" Then ThisDrawing.SelectionSets("BOM1").Delete Exit For End If Next If SourceDWG(i) <> "" Then MyNewPath = MyPath & SourceDWG(i) ThisDrawing.Application.Documents.Open (MyNewPath) ThisDrawing.SelectionSets.Add ("BOM1") Set Ssnew = ThisDrawing.SelectionSets("BOM1") Ssnew.Select acSelectionSetAll, , , IntGroupCode, VarGroupValue For Each Entity In Ssnew If TypeOf Entity Is AcadBlockReference Then Select Case Entity.Name Case "NUMFG" Numero = Entity.GetAttributes Numero(0).TextString = Str$(i) + 1 Case "NUMFGS" Numero = Entity.GetAttributes If i = 56 Then Numero(0).TextString = 56 Else Numero(0).TextString = Str$(i) + 2 End If End Select End If Next End If MyDestinationPath = (MyPath & "NuoviNumeri\" & DestinationDWG(i)) ThisDrawing.SaveAs (MyDestinationPath) ThisDrawing.Close Set Ssnew = Nothing Next End Sub
Hi,
>> but seems the error hang the routine in random point without a precise instruction
If you don't know where your routine crashes then:
If you have activated this option and your code runs into the exception the VBAIDE pops up and you see the code-line that makes troubles.
Good luck, - alfred -
Thank you for your support, I already done the code debug option when it hangs, but everytime the error appears during the execution of a differents parts of the code.
Thank you
Hi,
Different exception-locations but same error message? ...strange ;(
Does your pc also crash with non-VBA-procedures, so just while working within AutoCAD? Because the "different" locations and same test-drawings + same VBA-code looks to me like memory-error (when it comes to a defect memory-position it crashes).
You could use the On Error Resume Next options and do a check like If Err.Number <> 0 after each line of your existing code so maybe you come more close then with breakpoints within the if's or you can use this to output any log-file.
- alfred -
If you are processing many files, you can't assume they all will have the same conditions. For example, you create a filtered selection set and then attempt to iterate it contents using For...Each. What if the selection set is empty? The next line will fail on If TypeOf Entity, because Entity will be null. You need to check the selectionset count for =0 before you try to iterate it or check that Entity is not null. There could be other differences in the files as well. If some of the dwgs are old, the block definitions could vary and the attributes could vary. One thing you could do is trap the error and log the filename of the dwg that had the problem.
Rather than using On Error Resume Next, use structured error handling. Here's my standard template.
Public Sub ImportPlotConfigs() On Error GoTo ErrHandler: 'Do your work here. Exit Sub ErrHandler: Select Case Err.Number 'Add additional Case statements to meet your needs. Case 70, 75, -2147467259 MsgBox Err.Number & " - " & Err.Description & vbCrLf & "File may be in use or read-only.", vbCritical, "Import Page Setups" GoTo GetFile Case Else MsgBox Err.Number & " - " & Err.Description, vbCritical, "ImportPlotConfigs" End Select End Sub
Thank you very much for your support, I'll try to modify the code as per your suggestions.
Concerning the selections set it cannot be empty due to alla files contain at leat two blocks NUMFG,NUMFGS which are the numebr of pages and the page of the drawings set
I'll keep you update.
Thank you againg.
Hi,
I made modification you suggested to my code, but the problem it's always the same ! I got an automation error.
If I ask for a cdde debug and I excecute the code step by step with F8 function key I got any error.
Thank you for your support.
I didn't say it would prevent errors. It just handles them. You can make it handle them better, though. At first, you will a dialog for all errors. When you get one, note what error number it is and add a Case statement for it. Then you can decide how to handle the error if it occurs. You could display a different message, or go back to a line label and take a different acction, or as I said before, log the error to a log file.
Your last reply did not supply any useful information for us to help you. You should also post your modified code.
Hi,
>> For example, you create a filtered selection set and then attempt to iterate it contents using For...Each.
>> What if the selection set is empty? The next line will fail on If TypeOf Entity, because Entity will be null
If the selectionset has 0 elements, then the For Each ... Next loop does not execute any statement within the For ... Next, so the line you mentioned (with TypeOf) will not be touched.
- alfred -
You're right. That looks dumb now that you point it out. I think I was confusing myself with something similar I ran into a couple of weeks ago, but the conditions were different. "dazed and confused" - LED Zepplin
Hi,
I don't want to blame anyone here, my message was just for the OP that he does not try to search where there is nothing to look for. Sorry if you had another feeling with my post.
- alfred -
No problem, I'm fine. Thanks for catching my mistake.