The syntax is different between VBScript (used in GN3) and VB code (used in GN4). Here are some basic guidelines, and this example of a GN3 Tera library function and a GN3 user script, commented line-by-line.
See also Differences between GN4 and GN3 scripting.
Basic guidelines
•All variables have to be declared before using them, e.g. dim x.
•It is recommended to add the type to the dim statement, e.g. dim x as Integer.
•It is recommended to initialize variables when declaring them (where possible), e.g. dim x as Integer = 0.
•Avoid to declare variables inside branching, as Visual Studio Express syntax checker will complain, e.g.
if blah-blah then
dim x as Integer = 1
end if
msgbox (x)
In the red line, Visual Studio Express syntax checker complains that x is undeclared.
Correct code would be:
dim x as Integer = 0
if blah-blah then
x = 1
end if
msgbox (x)
•Use dim instead of set when declaring objects, e.g. dim obj = Text.GetParaStyle.
•All parameters are to be enclosed in parenthesis.
Function HeadSize (iStep, iMode, iMin, iMax) ' istep = change step in mpt ' iMode= -1 to decrease, 1 to increase ' iMin = min. allowed in mpt ' iMax = max. allowed in mpt f=72.27/72 iMinP=iMin*1004 iMaxP=iMax*1004 iOutOfRange = 0 if iMode = 1 and objstyle.Par(0) + iStep*iMode > iMaxP then iOutOfRange = 1 if iMode = -1 and objstyle.Par(0) + iStep*iMode < iMinP then iOutOfRange = -1 if iOutOfRange <> 0 then msgbox "Maximum point size for this style is reached: " & iif(ioutofrange=1, "max", "min") & " allowed " &_ (iif(ioutofrange=1,iMax,iMin)) else objstyle.Par(0) = objstyle.Par(0) + iStep*iMode Ted.SetParaStyle objStyle end if end function
Here is the Ted4 script: Set objStyle = Ted.GetParaStyle if objStyle.Name = "" then objStyle.Name = "head3" objstyle.Par(0) = Units.ToVal ("36pt") Ted.SetParaStyle objStyle end if
Headsize 1004, -1, 20000, 96000 |
Public Sub HeadSize(ByVal objstyle, ByVal iStep, ByVal iMode, ByVal iMin, ByVal iMax) ' istep = change step in mpt ' iMode= -1 to decrease, 1 to increase ' iMin = min. allowed in mpt ' iMax = max. allowed in mpt Dim f As Short = 72.27 / 72 Dim iMinP As Short = iMin * 1004 Dim iMaxP As Short = iMax * 1004 Dim iOutOfRange As Integer = 0 If iMode = 1 And objstyle.Par(0) + iStep * iMode > iMaxP Then iOutOfRange = 1 If iMode = -1 And objstyle.Par(0) + iStep * iMode < iMinP Then iOutOfRange = -1 If iOutOfRange <> 0 Then MsgBox("Maximum point size for this style is reached: " & IIf(iOutOfRange = 1, "max", "min") & " allowed " & _ (IIf(iOutOfRange = 1, iMax, iMin))) Else objstyle.Par(0) = objstyle.Par(0) + iStep * iMode Text.SetParaStyle(objstyle) End If End Sub
Public Sub EditResizeHeadInc() Dim objStyle = Text.GetParaStyle If objStyle.GetName = "" Then objStyle.SetName("head3") objStyle.SetPar(0, Units.ToVal("36pt")) Text.SetParaStyle(objStyle) End If HeadSize(objStyle, 1004, 1, 20000, 96000) End Sub |
Function HeadSize (iStep, iMode, iMin, iMax) |
Public Sub HeadSize(ByVal objstyle, ByVal iStep, ByVal iMode, ByVal iMin, ByVal iMax) |
1.A GN3 function is converted to a GN4 sub. A function has to return a value. It was optional in VBScript, but it is mandatory in VB. As original HeadSize does not return any value, we put it as sub. 2.If you want your method to be available in other add-ins, make it Public. 3.A parameter objstyle is added to Headsize, as in GN3 user script, it was passed implicitly, because called from the "main" code in user script. Main code is not available in VB, everything has to be either declaration or sub/function. Therefore, there was a need to pass the objstyle as parameter. 4.All parameters are automatically specified as ByVal. |
|
f=72.27/72 iMinP=iMin*1004 iMaxP=iMax*1004 iOutOfRange = 0 |
Dim f As Short = 72.27 / 72 Dim iMinP As Short = iMin * 1004 Dim iMaxP As Short = iMax * 1004 Dim iOutOfRange As Integer = 0 |
All variables have to be declared in VB. You can do it simply by adding dim before the variable, but the more correct way is to specify what kind of variable it is (Integer, Short, Boolean, Long, etc). |
|
if iMode = 1 and objstyle.Par(0) + iStep*iMode > iMaxP then iOutOfRange = 1 if iMode = -1 and objstyle.Par(0) + iStep*iMode < iMinP then iOutOfRange = -1
|
If iMode = 1 And objstyle.Par(0) + iStep * iMode > iMaxP Then iOutOfRange = 1 If iMode = -1 And objstyle.Par(0) + iStep * iMode < iMinP Then iOutOfRange = -1
|
No differences in this part |
|
msgbox "Maximum point size for this style is reached: " & iif(ioutofrange=1, "max", "min") & " allowed " &_ (iif(ioutofrange=1,iMax,iMin))
|
MsgBox("Maximum point size for this style is reached: " & IIf(iOutOfRange = 1, "max", "min") & " allowed " & _ (IIf(iOutOfRange = 1, iMax, iMin))) |
Concatenation is different: while VBScript required "&_" (no space between), VB requires "& space _". |
|
Ted4.SetParaStyle objStyle |
Text.SetParaStyle (objStyle) |
Number of methods are not anymore on Ted4 level, but on Text level. Parenthesis are mandatory. |
|
Public Sub EditResizeHeadInc() |
|
In GN4 VB, main code is not allowed anymore, everything has to be enclosed in functions/subs |
|
Set objStyle = Ted.GetParaStyle |
Dim objStyle = Text.GetParaStyle |
The definition of objects changed: no more set, always dim. |
|
if objStyle.Name = "" then objStyle.Name = "head3" objstyle.Par(0) = Units.ToVal ("36pt") Ted4.SetParaStyle objStyle end if |
If objStyle.GetName = "" Then objStyle.SetName("head3") objStyle.SetPar(0, Units.ToVal("36pt")) Text.SetParaStyle(objStyle) End If |
Some commands changed, e.g. objstyle.Name became objStyle.GetName; moreover, it's not anymore a property but a function, thus a new one is introduced objstyle.SetName. The same with Par => SetPar. Finally, Ted.SetParaStyle objstyle became Text.SetParastyle (objstyle) - the object name changed and the parenthesis are mandatory |
|
Headsize 1004, -1, 20000, 96000 |
HeadSize(objStyle, 1004, 1, 20000, 96000) |
Object is passed as parameter. Parenthesis are mandatory |