You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法通过VBScript创建关联文档问题求助

EA VBScript脚本创建需求关联文档失败的修复方案

以下是针对你的脚本问题的具体修复方案,直接解决关联文档无法生成的问题:

原脚本的核心问题

  1. EA对象模型缓存导致元素引用失效:创建元素后直接使用原newElement对象操作,可能因为缓存未同步导致SaveLinkedDocument不生效
  2. RTF格式不完整:缺少必要的段落结束标记,EA无法正确解析为有效关联文档

修复后的完整脚本

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 
Dim elementID As Long

' Get the running EA instance
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
elementID = newElement.ElementID ' 记录元素ID
currentPackage.Elements.Refresh

' 重新获取元素引用,避免缓存问题
Set newElement = currentPackage.Elements.GetByID(elementID)

' 完善RTF格式,添加段落标记确保EA正确解析
rtfText = "{\rtf1\ansi\deff0" & _
          "{\fonttbl {\f0 Arial;}}" & _
          "\f0\fs20 Hello from linked document!\par" & _ ' 添加\par段落结束标记
          "}"

' 保存关联文档
newElement.SaveLinkedDocument rtfText

' Provide a success message to the user.
MsgBox "New Requirement element with linked document created successfully!", vbInformation, "Done"

End Sub

' Execute the script
CreateRequirementWithLinkedDocument

关键修改说明

  • 重新获取元素引用:创建元素并Update后,通过元素ID重新从Package集合中获取对象,彻底规避EA的缓存同步问题
  • 完善RTF格式:在RTF文本末尾添加\par标记,这是EA解析关联文档时要求的必要格式,缺少会导致内容无法显示
  • 移除冗余Update:SaveLinkedDocument方法本身会自动保存关联文档,无需额外调用newElement.Update

验证方法

运行脚本后,在EA项目浏览器中找到新建的需求元素,打开其关联文档标签页,即可看到正确显示的富文本内容。

内容的提问来源于stack exchange,提问作者Mikkie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 23:48:25