简体   繁体   中英

Using Staad with VBS (Convert VBA documentation to VBscript)

I am trying to use a vbscript to pull data from Staad (a structural analysis program).

Staad has an API to allow this, however, all the documentation is in VBA. So I'm trying to convert the VBA to VBscript, but I'm getting errors.

For example, here is some VBA from the Staad documentation for getting the total number of nodes in your model:

Dim objOpenSTAAD As Output
Dim pnNodes As Integer
Set objOpenSTAAD = CreateObject("OpenSTAAD.Output.1")
objOpenSTAAD.SelectSTAADFile "C:\SPRO2003\STAAD\Examp\US\examp08.std"
objOpenSTAAD.GetNodesCount pnNodes

I've tried running this as a vbscript, the only change I made was to remove the data types from the variables. The error I'm getting is:

Type mismatch: 'GetNodesCount'

Can anyone offer any ideas? In case it helps, here is the Staad documentation for the GetNodesCount function:

GetNodesCount

VB Syntax

integer GetNodesCount (integer pnNodes)

Parameters

pnNodes

An integer variable for storing the number of nodes retrieved by the function.

Remarks

This function retrieves the number of nodes in the currently open STAAD file.

Example

Dim pnNodes As Integer

objOpenSTAAD.GetNodesCount pnNodes

The problem with your code is probably that you aren't assigning anything to pnNodes . In VBA, that's okay because it's strongly-typed as an Integer which means it's given a default value of 0 implicitly. In VBScript, it's not given a default value because there is no type. You could assign it a 0 ahead of time and it would probably work:

Dim objOpenSTAAD As Output
Dim pnNodes As Integer
Set objOpenSTAAD = CreateObject("OpenSTAAD.Output.1")
objOpenSTAAD.SelectSTAADFile "C:\SPRO2003\STAAD\Examp\US\examp08.std"
pnNodes = 0
objOpenSTAAD.GetNodesCount pnNodes

Most likely, though, your intention is not to have this value be 0 (I'm just guessing), which means this conversion process has helped you find a bug :)

Using CInt(pnNodes) as a parameter passes a function's result, not a writable variable, to .GetNodesCount. Voodoo, but easy to test:

Dim objOpenSTAAD : Set objOpenSTAAD = CreateObject("OpenSTAAD.Output.1")
Dim pnNodes : pnNodes = CInt(0) ' initialized to sub type Integer
Dim nNodes ' uninitialized (Empty)
objOpenSTAAD.SelectSTAADFile "C:\SPRO2003\STAAD\Examp\US\examp08.std"
objOpenSTAAD.GetNodesCount pnNodes ' may work
nNodes = objOpenSTAAD.GetNodesCount(pnNodes) ' fallback

Update wrt comment:

As you said using objOpenSTAAD.GetNodesCount CInt(pnNodes) "got rid of the error", try

nNodes = objOpenSTAAD.GetNodesCount(CInt(pnNodes)) 

and hope that nNodes gets the result that couldn't be written to CInt(pnNodes) .

On third thought:

According to this post , the required data type for the pnNodes parameter may be Long (instead of Integer). So try the above variations with CLng instead of CInt .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM