我正在尝试使用运行时对象从Excel自动执行 Bentley Microstation 而无需项目参考来进行概念验证。
Sub MSFromXlRuntime()
Dim MS As Object
Dim DesignFile As Object
Dim Line As Object
Dim startPoint As Variant
Dim endPoint As Variant
Set MS = CreateObject("MicroStationDGN.Application")
MS.Visible = 1
Set DesignFile =
MS.OpenDesignFile("C:\mydgn.dgn", False)
startPoint = MS.Point3dFromXYZ(2#, 2#, 0#)
endPoint = MS.Point3dFromXYZ(4#, 4#, 0#)
Set Line = MS.CreateLineElement2(Nothing, startPoint, endPoint)
MS.ActiveModelReference.AddElement Line
End Sub
以上代码在线生成Run time Error '13' Type Mismatch
Set Line = MS.CreateLineElement2(Nothing, startPoint, endPoint)
在本地窗口中,变量startPoint
和endPoint
的类型显示为Variant/Point3d
,这是CreateLineElement2()
函数所期望的。
如何解决这个问题?
关注评论的最后一次尝试
重新定义用户数据类型Point3d也会出现如下错误。这种情况下的错误是:
编译错误: 只有在公共对象模块中定义的用户定义类型才能被强制转换为变体或从变量强制转移或传递给后期绑定函数
Type myPoint3d
X As Double
Y As Double
Z As Double
End Type
Sub MSFromXlRuntime()
Dim MS As Object
Dim DesignFile As Object
Dim Line As Object
Dim startPoint As myPoint3d
Dim endPoint As myPoint3d
Set MS = CreateObject("MicroStationDGN.Application")
MS.Visible = 1
Set DesignFile = MS.OpenDesignFile("C:\mydgn.dgn", False)
startPoint.X = 2#: startPoint.Y = 2#: startPoint.Z = 0#
endPoint.X = 4#: endPoint.Y = 4#: endPoint.Z = 0#
Set Line = MS.CreateLineElement2(Nothing, startPoint, endPoint)
MS.ActiveModelReference.AddElement Line
End Sub
我想也许我正在尝试的东西在VBA中是不可能的。我应该坚持以通常的方式导入项目引用。