Useful WordBasic commands that have no VBA equivalent

Article contributed by Jonathan West

When Microsoft released Word 97, a new programming language VBA replaced the WordBasic language that had been available in earlier versions of Word. For most things, VBA is a much more powerful and flexible programming language than WordBasic, but there are a few very useful WordBasic commands which have no direct equivalents in  VBA.

Fortunately, VBA includes the WordBasic object, which gives access to most of the old WordBasic commands.
  

SortArray

This is perhaps the most useful of the commands left behind. It allows you to sort the elements of an array using a single line of code. At its simplest, you can use it on a one-dimensional array as follows.

Sub SortTest()
    Dim ss(2) As String
    Dim i As Long

    ss(0) = "orange"
    ss(1) = "apple"
    ss(2) = "banana"
    WordBasic.SortArray ss()

    For i = 0 To 2
        Debug.Print ss(i)
    Next i

End Sub

This sorts the array in ascending alphabetical order

However, you can also sort in descending order, and sort either dimension of a two-dimension array. The full list of the SortArray arguments is as follows

SortArray ArrayName[$]() [, Order] [, From] [, To] [, SortType] [, SortKey]

 

ArrayName is the name of the array

 

Order is 0 for ascending (by default), 1 for descending

 

From is the first element to sort (0 by default)

 

To is the last element to sort (by default the last element of the array)

 

SortType determines whether you are sorting rows or columns. 0 (default) for rows, 1 for columns

 

SortKey is applicable only to two-dimensional arrays, and indicates the row or column used as the sort key. It is 0 by default

Note that, unlike most VBA methods, you don't use named arguments with this command; thus you can have

WordBasic.SortArray MailingList$(), 1, 1, 20, 0, 1

but not

WordBasic.SortArray ArrayName:=MailingList$(), Order:=1, From:=1, To:=20, _
        SortType:=0, SortKey:=1

Also, you cannot miss out arguments if you want to use later ones, thus you
can have

WordBasic.SortArray Test(), 0, 0, 2, 0, 1

but not

WordBasic.SortArray Test(), 0, , , , 1

There is one other limitation of the SortArray command. It will sort an array declared as such, but it will not sort an array that is contained in a Variant. If you create an array like this:

Dim vArray as Variant
vArray = Array("orange", "apple", "banana")

SortArray will not sort it. 

(Also if you do not declare your array at all, it will be treated as a variant and will not be sorted).
  

FileNameInfo$()

This is another very useful function for which there is no direct VBA equivalent. FileNameInfo allows you to get just the filename or a fully qualified pathname from a filename given to it. The nearest equivalent in VBA are the Name, FullName and Path properties of the Document object.

FileNameInfo is different in that you don't need to have the document open.

The syntax is

x = WordBasic.FilenameInfo$(Filename$, FileType)

where Filename is the name of the file, and FileType is a number which defines the  part of the filename you want to return:

1 - the full pathname, e.g. C:\My Documents\My File.doc"
2 - the filename only, if the file is in the current folder, otherwise the full pathname
3 - the filename only
4 - the filename without the extension
5 - the path without the filename
6 - the UNC pathname

One case where FileNameInfo$ is very useful is to get the pathname of a file which has just been selected by the user in the FileOpen dialog. The following code returns the full pathname of a file selected by the user.

With Dialogs(wdDialogFileOpen)
    If .Display Then
        MsgBox WordBasic.FilenameInfo$(.Name, 1)
    Else
        MsgBox "No file selected"
    End If
End With
  

DisableAutoMacros

If you are running a macro that opens (or creates) several files, the last thing you may want is for an AutoOpen (or AutoNew) macro to fire up each time. WordBasic has a means of preventing this, which VBA never copied.

WordBasic.DisableAutoMacros 1   'Disables auto macros
WordBasic.DisableAutoMacros 0   'Enables auto macros

This command is also very useful when launching an instance of Word from another application, or from VB, when you will generally not want any AutoExec macros to fire.
  

ToolsBulletsNumbers

WordBasic allows you to remove all manually typed numbering from a selection using the old Word 2 command:

WordBasic.ToolsBulletsNumbers Replace:=0, Type:=1, Remove:=1

This is particularly useful for removing manually typed numbering from Headings in a document you have been emailed, prior to applying List Numbering. If you go into Outline View, set the Heading Level to the number of levels you need to remove the typed numbering from, and run the above line, it will just remove numbering from those Headings and will leave the body text alone. Or you can use the following macro to do the same thing:

Sub RemoveNumbersFromHeadings()

Dim
ViewType As Long, ShowHeadingLevel As Long, MyRange As Range

Application.ScreenUpdating = False
'Set Range variable to current selection so it can be returned to at the end
Set MyRange = Selection.Range
'Set variable to current View so it can be returned to at the end
ViewType = ActiveWindow.View.Type
'Switch to Outline View
ActiveWindow.View.Type = wdOutlineView

'Checks the state (using the ID in case the toolbar has been customised) of _
all the ShowHeadings buttons on the Outline toolbar; if none are depressed, _
then it must be set to ShowAll. Stores result in a variable so that _
the same level can be returned to at the end of the macro


For
ShowHeadingLevel = 71 To 77
    If CommandBars.FindControl(ID:=ShowHeadingLevel).State Then
        ShowHeadingLevel = (ShowHeadingLevel - 70)
        Exit For
    End If
Next ShowHeadingLevel
'if none of the heading level buttons depressed sets variable to 1
If ShowHeadingLevel = 78 Then ShowHeadingLevel = 1

ActiveWindow.View.ShowHeading 3
ActiveDocument.Select

WordBasic.ToolsBulletsNumbers Replace:=0, Type:=1, Remove:=1

If ShowHeadingLevel = 0 Then
    ActiveWindow.View.ShowAllHeadings
Else
    ActiveWindow.View.ShowHeading ShowHeadingLevel
End If

ActiveWindow.View.Type = ViewType
MyRange.Select

Set MyRange = Nothing

Application.ScreenUpdating = False

End Sub
  

FileCopy/FileCopyA

The VBA FileCopy statement will not copy files that are open. However, the WordBasic equivalent will (this is what is known as progress!).

Unfortunately, the syntax of WordBasic equivalent is different in Word 97 and Word 2000!

The following works even if the file being copied is open:

If Left$(Application.Version, 1) = "8" Then
    'Word 97
   
WordBasic.CopyFile FileName:="c:\OldDirectory\Temp.doc", _
    Directory:="C:\NewDirectory\Temp.doc"
Else
    'Word 2000 and above
    WordBasic.CopyFileA FileName:="c:\OldDirectory\Temp.doc", _
            Directory:="C:\NewDirectory\Temp.doc"
 End If

FileProperties

If you want to intercept the FileProperties menu command, the only reliable way to do it is to use:

Sub FileProperties()
    'your code here
    WordBasic.FileProperties
End Sub

In fact, if you let Word create the code for you, using the method described here, the above code will be created.
  

SendKeys

If you want to do a screen capture using VBA, (simulating the PrtScr key), you have to use:

WordBasic.SendKeys "%{1068}"
  

Download WordBasic Help File

Microsoft has made the old WordBasic help file available online. For further information about WordBasic commands, you can download it here..


Click to view Terms of Use page

Click to view Disclaimer page