Variable Error Handling and Deletion

Variable Error Handling and Deletion

dbrblg
Collaborator Collaborator
1,492 Views
1 Reply
Message 1 of 2

Variable Error Handling and Deletion

dbrblg
Collaborator
Collaborator

I am using this code to open a file dialog:

Public Sub OpenDialog1()
   Dim fileName As String
   'Using the SendCommand method, send getfiled AutoLISP expressions to the AutoCAD command line.
   'Set the return value to a user-defined system variable USERS1.
   ThisDrawing.SendCommand "(setvar " & """users1""" & "(getfiled " & """Select a DWG File""" & """c:/program files/acad2012/""" & """dwg""" & "8)) "
   'Use the GetVariable method to retrieve this system variable to store the selected file name
   fileName = ThisDrawing.GetVariable("users1")
   MsgBox "You have selected " & fileName & "!!!", , "File Message"
End Sub

If I click cancel, I get an error message:

Run-time error '-2145320859 (80210065)':
Error getting system variable

How can I trap this?  Are there any functions specifically designed for this? 

 

Secondly, when the sub has finished, I'd ideally like to clean out any variables. Is there a delvar or similar to get rid of these temporary variables when I don't need them?

 

Thanks

0 Likes
Accepted solutions (1)
1,493 Views
1 Reply
Reply (1)
Message 2 of 2

norman.yuan
Mentor
Mentor
Accepted solution

That is because the function (getfiled ...) returns nil if user clicks the "Cancel" button to close the file dialog, while  system variable "USERS1/2/3/4/5" cannot be nil (empty string value is not the same as nil in LISP). So, you need to modify your LISP code so that the (getfiled...)'s return value would be assigned to a generic variable, then test whether its value is nil or not. Something like:

 

(if (setq selFile (getfiles .....))

  (setvar "USERS1" selFile)

  (setvar "USERS1" "")

)

 

After this, in your VBA code, you can then test variable "USERS1" to see if it is a valid file name or an empty string, indicating a file name is selected or not.

 

HTH

 

Edit: regarding your second question: after you are done with USERS1, you can simply set it back to empty string. Ideally, before you user it, you should save whatever value in a VBA's string variable (in case other programs loaded into AutoCAD uses it). And when you are done, set it back to original value, instead of blindly set it to empty string.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes