You can use rtitem.AppendDocLink(doc, "Title")
to create the doc link you want.
There is a little challenge to get a satisfying solution. If we put the following lines into button which creates the new document
...
Set docSelected = session.DocumentContext
Set docNew = dbTarget.CreateDocument()
Set rtitem = docNew.CreateRichTextItem("Doclink")
Call rtitem.AppendDocLink(docSelected, "Title")
Call docNew.Save(True, True) ' <--- that is necessary :(
...
Call workspace.EditDocument(True, docNew)
then it creates a link in new document and it is visible immediately. BUT, it works only if we save the new document. This might be not a good idea because the document stays in database even when the user just closes the document without saving.
So, we have to look for another solution.
Let's turn in around and let the new document create the doc link after it was saved by user. For this, we have to give the new document the information about database and UniversalID of the link document. We do that in button which creates the new document
Set docNew = dbTarget.CreateDocument()
docNew.LinkDb = session.CurrentDatabase.FilePath
docNew.LinkUniversalID = session.DocumentContext.UniversalID
In new document's form we create a computed RichText field "Doclink". When is a good time to fill the field with the link? As I figured out is the Querysave
event too early. The field "Doclink" gets damaged during standard document save. The solution is to wait until Queryclose
and to set the link there. But, we should to do it only if user saved the document and only one time. That's why we have to monitor the Querysave
event and set a flag there that user saved document. At Queryclose
we create the link, delete the fields "LinkDb" and "LinkUniversalID" and save the document again if the save flag is set and document contains field "LinkDb".
Here is the code for form:
(Declarations)
Dim bSaved As Boolean
Sub Initialize
bSaved = False
End Sub
Sub Querysave(Source As Notesuidocument, Continue As Variant)
bSaved = True
End Sub
Sub Queryclose(Source As Notesuidocument, Continue As Variant)
If bSaved Then
Dim session As New NotesSession
Dim doc As NotesDocument
Dim dbLink As NotesDatabase
Dim docLink As NotesDocument
Dim rtitem As NotesRichTextItem
Set doc = Source.Document
If doc.HasItem("LinkDb") Then
Set dbLink = session.GetDatabase(doc.ParentDatabase.Server, doc.LinkDb(0), False)
Set docLink = dbLink.GetDocumentByUNID(doc.LinkUniversalID(0))
doc.RemoveItem("Doclink")
Set rtitem = doc.CreateRichTextItem("Doclink")
Call rtitem.AppendDocLink(docLink, "Link")
doc.RemoveItem("LinkDb")
doc.RemoveItem("LinkUniversalID")
Call doc.Save(True, True)
End If
End If
End Sub
It is important to delete the RichText field and create it new before we add the doc link.