0

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

1 Answer 1

0

It seems you have misunderstood the purpose of the method SaveLinkedDocument

In the manual it says

Boolean

Notes: Saves the Linked Document for this element to the specified file. Returns False if the element does not have a Linked document or fails to save the file.

Parameters:

FileName: String - the name of the file to save to disk The output format will be determined by the file's extension - currently rtf, docx and pdf are supported; if an invalid extension is used, it will write the file in RTF format regardless of the extension

The only method available to update the contents of the linked document is LoadLinkedDocument(string Filename)

To use this you'll need to create a temporary .rtf file, and pass the filename into this method.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.