I have this script to create a requirement and a linked document attached to that requirement. For the life of me, I cannot get the linked doc created on the requirement.
The code creates a single requirement, then adds some text to the notes section.
It then creates a linked document and adds some rich text to the linked document, saves the linked document and then updates the element.
Option Explicit
'---------------------------------------------------------------------
' Script Name: CreateRequirementWithLinkedDocument
' Description: Creates a requirement element in the currently selected
' package, attaches a linked document, and adds some RTF text.
'---------------------------------------------------------------------
Sub CreateRequirementWithLinkedDocument()
Dim repository As EA.Repository
Dim currentPackage As EA.Package
Dim newElement As EA.Element
Dim rtfText
' Get the running EA instance (assuming the script is run inside EA).
' If you are running this script *outside* EA, you would need to use
' GetObject(, "EA.App") or CreateObject("EA.App") and then get Repository.
Set repository = App.Repository
' Get the currently selected package in the Project Browser.
Set currentPackage = repository.GetTreeSelectedPackage()
If currentPackage Is Nothing Then
MsgBox "No package is currently selected. Please select a package first.", vbExclamation, "Error"
Exit Sub
End If
' Create a new Requirement element in the selected package.
Set newElement = currentPackage.Elements.AddNew("My New Requirement", "Requirement")
newElement.Notes = "Notes for my new requirement..."
newElement.Update
currentPackage.Elements.Refresh
' Minimal RTF text. The \rtf1 and \ansi tokens are required, along with some font info.
' This sample simply displays the text "Hello from linked document!" in Arial, size 10.
rtfText = "{\rtf1\ansi\deff0" & _
"{\fonttbl {\f0 Arial;}}" & _
"\f0\fs20 Hello from linked document!" & _
"}"
' Save the RTF into the element's linked document.
newElement.SaveLinkedDocument rtfText
newElement.Update
' Provide a success message to the user.
MsgBox "New Requirement element with linked document created successfully!", vbInformation, "Done"
End Sub
' Execute the script
CreateRequirementWithLinkedDocument