If you are using that code 50-100 times per day for quite a while and only had a problem once, that is pretty good performance. Is it possible that one of the files being accessed by the code at that point in time was also being accessed by another person, or some other application on your computer when that error happened? If so, that one file may have temporarily acted like ReadOnly, causing the error. The initial check of the 'IsModifiable' property, suggested by Michael, would be the best option to avoid that potential issue. However, checking the value of that property requires having a reference to the actual Document object that you will be accessing the iProperties of within that Sub routine. So, maybe the next best suggestion may be to send the document object to that Sub routine. But then, since you would already have a reference to the Document readily available, I would suggest switching from using that iLogic API line of code, to using the Inventor API way of accessing its properties, to avoid the potential issue of wrong document being accessed.
If you are looking for ways to improve your existing code, either to avoid all potential errors, or to improve efficiency, or both, then there are lots of ideas. It all depends on what data is available at that point in your code, and your coding style. Using the iLogic API tools usually requires less code overall, but I am not sure if they are the most efficient, or fastest, or the most clear about how they will function in some situations. I like using iLogic API tools in small, internal rules, but usually prefer the certainty of the Inventor API tools in my larger &/or more complex &/or external rules. You may, or may not be aware of this, but when you use a line of iLogic API code like that, it causes an alternate block of code defined within the Inventor add-in resources to get ran in the background, where it is most likely primarily utilizing the Inventor API code anyways. One of the differences of using iLogic API tools is that those tools generally require us to specify the 'names' of objects, instead of references to the actual objects themselves, which usually requires less code. This does not mean that less 'work' or processing is being done, but often actually more, because it must first get access to the actual objects it needs, using those supplied names to find them, before it can do its primary task using Inventor API code internally.
Another thing that may help with performance (if that is a goal) is avoiding the use of Try...Catch...End Try statements, in favor of one or more simple If...Then statements, when the If...Then statements are sufficient to avoid an expected/potential error.
Some possible alternatives utilizing Try...Catch...End Try statements:
(shortest / simplest, but least control, and no feedback)
Sub UpdateMatrProperty(value As String)
Try : iProperties.Value("Custom", "Raw Material") = value : Catch : End Try
End Sub
or (using nested Try statements, but still no feedback)
Sub UpdateMatrProperty(oDoc As Document, sValue As String)
Try
oDoc.PropertySets.Item(4).Item("Raw Material").Value = sValue
Catch
Try : oDoc.PropertySets.Item(4).Add(sValue, "Raw Material") : Catch : End Try
End Try
End Sub
or (more control, no nested Try statements, with minimal feedback)
Sub UpdateMatrProperty(oDoc As Document, sValue As String)
If (Not oDoc.IsModifiable) Then
Logger.Debug(oDoc.DisplayName & " is not modifiable!")
Return
End If
Dim oProp As Inventor.Property = Nothing
Try : oProp = oDoc.PropertySets.Item(4).Item("Raw Material") : Catch : End Try
If oProp Is Nothing Then
Try : oProp = oDoc.PropertySets.Item(4).Add(sValue, "Raw Material") : Catch : End Try
Else
Try : oProp.Value = sValue : Catch : End Try
End If
End Sub
There are lots of possibilities for how this could be done differently, including actually iterating through the custom properties checking property names to find the existing one. If found, set its value. If not found during iteration, then create it after the iteration. The initial 'IsModifiable' check should avoid any other errors from happening.
Wesley Crihfield

(Not an Autodesk Employee)