Jfp87
05-02-2017, 12:53 AM
Guys,
I am adding a document variable and I am specifically handling the case where the variable already exists in the document (code is just a snippet):
On Error Resume Next
Err.Clear
oDoc.Variables.Add VariableName, VariableValue
If Err.Number = 5903 Then
''''''''''''''''''''''''''''''''''''''''
'The variable name already exists.
'Edit the existing variable value.
''''''''''''''''''''''''''''''''''''''''
oDoc.Variables(VariableName).Value = VariableValue
Err.Clear
End If
On Error GoTo 0
I'm pretty sure that the above is sufficient for almost everything however, after having a think about it, is the following better? (snippet):
oDoc.Variables.Add VariableName, VariableValue
Select Case Err.Number
Case 0
'''''''''''''''''''''''''''''''''''''''''''
'No error. Do nothing - variable was added.
'''''''''''''''''''''''''''''''''''''''''''
Case 5903
'''''''''''''''''''''''''''''''''''''''''''
'Variable name already exists. Edit existing.
'''''''''''''''''''''''''''''''''''''''''''
oDoc.Variables(VariableName).Value = VariableValue
Case Else
'''''''''''''''''''''''''''''''''''''''''''
'An unanticipated error occurred - exit.
'''''''''''''''''''''''''''''''''''''''''''
Err.Clear
Exit Function
End Select
I'm not sure if it's overkill but the way I see it, only 1 of 3 things are going to happen:
1) No error (0)
2) Variable exists (5903)
3) Something else
I'm not even sure if it is possible for another error to occur, but I thought it was more robust to deal with it in that way. Any suggestions?
Cheers,
Joe
I am adding a document variable and I am specifically handling the case where the variable already exists in the document (code is just a snippet):
On Error Resume Next
Err.Clear
oDoc.Variables.Add VariableName, VariableValue
If Err.Number = 5903 Then
''''''''''''''''''''''''''''''''''''''''
'The variable name already exists.
'Edit the existing variable value.
''''''''''''''''''''''''''''''''''''''''
oDoc.Variables(VariableName).Value = VariableValue
Err.Clear
End If
On Error GoTo 0
I'm pretty sure that the above is sufficient for almost everything however, after having a think about it, is the following better? (snippet):
oDoc.Variables.Add VariableName, VariableValue
Select Case Err.Number
Case 0
'''''''''''''''''''''''''''''''''''''''''''
'No error. Do nothing - variable was added.
'''''''''''''''''''''''''''''''''''''''''''
Case 5903
'''''''''''''''''''''''''''''''''''''''''''
'Variable name already exists. Edit existing.
'''''''''''''''''''''''''''''''''''''''''''
oDoc.Variables(VariableName).Value = VariableValue
Case Else
'''''''''''''''''''''''''''''''''''''''''''
'An unanticipated error occurred - exit.
'''''''''''''''''''''''''''''''''''''''''''
Err.Clear
Exit Function
End Select
I'm not sure if it's overkill but the way I see it, only 1 of 3 things are going to happen:
1) No error (0)
2) Variable exists (5903)
3) Something else
I'm not even sure if it is possible for another error to occur, but I thought it was more robust to deal with it in that way. Any suggestions?
Cheers,
Joe