Dim ws As New notesUIWorkspace
Dim uiDoc As notesUIDocument
Set uiDoc = ws.currentDocument
On Error Resume Next
Call uiDoc.Save
On Error Goto 0
If Err <> 0 Then
Err = 0
Exit Sub
End If
' ... Continue with your code ...
But what if you didn't really want to save that early in your code? You want to set some other fields first and then save? Well, there's a more complicated way to accomplish the methods outlined above.
First, you are going to create a computed for display, text, field that will add some control to the validation formulas. Let's call the field "FlagRefresh". The field can be hidden all the time and it doesn't matter where the field appears on your form. The value for this field will be:
@If(@IsDocBeingLoaded; "0"; FlagRefresh)
What this does is initially set the field to a text zero when the document is opened, and then leaves it alone from that point on.
Next, you'll need to update your validation formulas. Let's give the example again of checking for the field called "Untitled" to be blank:
@If(@IsDocBeingSaved; @If(Untitled = ""; @Failure("Please fill out this field"); @Success); FlagRefresh = "0"; @Success; Untitled = ""; @Failure("Please fill out this field"); @Success)
Hopefully you see what's going on. If the document is being saved, we go through the validation formulas. Then we check to see if that new field is set to "0", which will be the case if the user has pressed F9 or the document is refreshing because of a keyword change, or if you called uiDoc.Refresh in a script. If that's the case, then the validation succeeds. Lastly (the document is not being saved and the FlagRefresh field is not zero), we go through the validation again.
Note that we have to put the @IsDocBeingSaved in the formula because simply checking FlagRefresh being zero would allow the document to be saved without checking validation formulas.
NOTE: You might have thought you could get around that by changing FlagRefresh to "1" if the document is being saved. Like this formula: @If(@IsDocBeingLoaded; "0"; @IsDocBeingSaved; "1"; FlagRefresh). That wouldn't entirely work. Once the user tries to save the document, the field will be set to "1" and remain at "1" even if the document doesn't save. So that means that if the user presses F9 after that point (or if they change a keyword value which causes the document to refresh), all validation formulas will be run through because FlagRefresh is still "1". So it must be done the way that is outlined above.
Now, the script code changes slightly:
Dim ws As New notesUIWorkspace
Dim uiDoc As notesUIDocument
Dim doc As notesDocument
Set uiDoc = ws.currentDocument
Set doc = uiDoc.document
Call doc.replaceItemValue("FlagRefresh", "1")
On Error Resume Next
Call uiDoc.Refresh
On Error Goto 0
Call doc.replaceItemValue("FlagRefresh", "0")
If Err <> 0 Then
Err = 0
Exit Sub
End If
' ... Continue with your code ...
The difference here is that we set the "FlagRefresh" field in the back-end document. Then when the Refresh method is called, the field has been set to "1" which will cause the validation formulas to exectue even though we only called the Refresh method. After calling the Refresh method, the field is set back to "0" for the next refresh by the user or by a keyword field changing. Again, if there was an error trapped trying to refresh, the code quits. If there wasn't an error the code continues and you can do whatever you want.