VbScriptXtra for Macromedia DirectorHow to translate VB macro into Lingo?
The simplest way of finding how to do something with Office applications (Word, Excel, PowerPoint etc.) is recording a macro. It gives you an idea which objects you should use to achieve what you want to do.
For example, we want to automate some search operations in Word. After recording search operation as a macro we may look at the VB code. It will look something like this:
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 02.07.2006
'
Selection.Find.ClearFormatting
With Selection.Find
.Text = "some text to find"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
End Sub
Lingo syntax is a bit different, so there are some check points below that you should know to translate this VB code into VbScriptXtra's Lingo.
Selection.Find.ClearFormatting
In VB macro Application object is assumed by default, so in Lingo you should refer it explicitly:
app = xtra("VbScriptXtra").CreateObject("Word.Application")
app.Selection.Find.ClearFormatting()
If you have only document reference of the OLE object then you may retrieve the application object from the document object:
doc = sprite(1).GetObject()
app = doc.application
With Selection.Find
This construction makes the Selection.Find to be the default scope until End With statement. So, it is similar to:
objFind = app.Selection.Find
objFind.Text = "some text to find"
etc...
.Wrap = wdFindContinue
It is a named value. VbScriptXtra supports the simple translation to Lingo symbols:
objFind.Wrap = #wdFindContinue
Also you can use GetEnum method to retrieve the value of the named value:
put objFind.GetEnum("wdFindContinue")
Selection.Find.Execute
Here you just execute the search operation. In Lingo it requires parenthesis:
objFind.Execute()
By the way, it is a good idea to look for it with ObjectBrowser. Call:
objFind.interface() to see other methods and properties of this object.
Selection
Recorded macroses offer you operations with selection, because that is what you are doing while macro is recorded. Normally you do not need to use Selection object at all. For example search and replace operations are available from any Range object. For example:
objFind = doc.Range().Find
Macro is useful to see which objects, methods or properties you should use to achieve the goal. It is a good idea to see what documentation says about these objects, methods and properties. (Make sure you have installed the 'Visual Basic Documentation' with the Office, since it may be excluded by default installation). Macro is just a kind of starting point.
Named arguments
In VB you may call obj.SomeMethod with so called named arguments:
obj.SomeMethod NameOfSomeArg:="SomeValue"
You should see the definition of this method and check the ordinal number of <NameOfSomeArg>. If it is, for example, the third one, then in Lingo it may look like:
obj.SomeMethod( VOID, VOID, "SomeValue" )
Otherwise you may use new feature of VbScriptXtra 2.3.3. Put named arguments into a property list:
obj.SomeMethod( [ #NameOfSomeArg:"SomeValue" ] )
See other VbScriptXtra samples of Word, Excel and PowerPoint usage.
|