WD97: Writing Macro Code for VBA and WordBasic Branching

This article was previously published under Q163618
This article has been archived. It is offered "as is" and will no longer be updated.
This article includes sample Visual Basic code that demonstrates how todetermine the version of Word for Windows currently installed when usingOLE automation. Based on the version of Word, the code will conditionallybranch to use WordBasic commands if the version of Word is earlier thanWord 97. If the version of Word returned is equal to or greater than Word97, the code will branch to use Visual Basic for Applications commands.

You may want to use this conditional branching if you are using more thanone version of Word on your computer.
In Microsoft Word 97 and later versions, WordBasic has been replaced withVisual Basic for Applications. For backward compatibility, Visual Basic forApplications contains a WordBasic object. To ensure that the WordBasiccommand and arguments are positioned correctly, use the Position.hlp filefor Word for Windows version 6.x.

NOTE: If your programming language was written to call WordBasic commandsand you are anticipating interacting with the Microsoft Word Visual Basicfor Applications language for all future development needs, you shouldconvert your code from WordBasic to Microsoft Word Visual Basic forApplications.

Microsoft provides examples of Visual Basic for Applications proceduresfor illustration only, without warranty either expressed or implied,including, but not limited to the implied warranties of merchantabilityand/or fitness for a particular purpose. The Visual Basic procedures inthis article are provided 'as is' and Microsoft does not guarantee thatthey can be used in all situations. While Microsoft support professionals canhelp explain the functionality of a particular macro, they will not modifythese examples to provide added functionality, nor will they help youconstruct macros to meet your specific needs. If you have limitedprogramming experience, you may want to consult one of the MicrosoftSolution Providers. Solution Providers offer a wide range of fee-basedservices, including creating custom macros. For more information aboutMicrosoft Solution Providers, call Microsoft Customer Information Serviceat (800) 426-9400.
 ' ******************************************************************   ' MODULE NAME: Coding For WordBasic or Visual Basic Based On Version   ' DECLARATIONS SECTION   ' ******************************************************************   Dim oWordWB As Object   Dim oWordVBA As Object   ' The GetWordVersion routine creates a WordBasic object. It then   ' determines the currently installed version of Word for Windows.   ' If the version is earlier than Word 97, it branches to use a   ' WordBasic Code" routine.   ' If the version is Word 97 or later, it branches to use a Visual   ' Basic for Applications routine. If Word is not currently installed,   ' an error is trapped and the program displays a message and ends.   Sub GetWordVersion ()      ' *******************************************************      ' PURPOSE: To determine the Word version and branch      '          conditionally based on the version returned.      '      ' ARGUMENTS:      '           oWordWB - WordBasic Object      ' *******************************************************      On Error GoTo NoWord      Set oWOBJ = CreateObject("Word.basic")      If Val(oWOBJ.AppInfo(2)) < 8 Then         ' If earlier than version 8, branch to WordBasic code.         UseWordBasicCode      Else         ' If version 8 or later, branch to Visual Basic for         ' Applications code.         UseVBACode      End If   NoWord:      If Err <> 0 then         MsgBox "Word is not installed."         End      End If   End Sub      Sub UseVBACode ()         ' *********************************************************         ' PURPOSE: Used for Visual Basic For Applications commands.         '         ' ARGUMENTS:         '           oWordWB  - WordBasic Object         '           oWordVBA - Visual Basic for Applications Object         ' *********************************************************         ' Close the WordBasic Object that was used to determine the         ' Word version.         oWordWB.FileExit         Set oWordWB = Nothing         ' Create a Word Visual Basic For Applications object.         Set oWordVBA = CreateObject("Word.Application")         ' ***********************************************         ' < Visual Basic For Applications Commands Here >         ' ***********************************************         ' When quitting an OLE session with Visual Basic For Applications,         ' you must use the Quit command in combination with setting         ' the OLE object to Nothing to clear the instance from memory.         oWordVBA.Quit         Set oWordVBA = Nothing      End Sub      Sub UseWordBasicCode ()         ' *************************************         ' PURPOSE: Used for WordBasic commands.         '         ' ARGUMENTS:         '           oWordWB - WordBasic Object         ' *************************************         ' **************************         ' < WordBasic Commands Here>         ' **************************         ' Close the instance of Word.         oWordWB.FileExit         ' Clear the Object from memory.         Set oWordWB = Nothing      End Sub				
wordcon vb vbe vba

Article ID: 163618 - Last Review: 01/11/2015 16:42:24 - Revision: 1.1

  • Microsoft Word 97 Standard Edition
  • kbnosurvey kbarchive kbcode kbfaq kbhowto kbinterop kbprogramming KB163618