VBA – Visual Basic for Application that is built especially for Microsoft office including Word, Excel, PowerPoint, Outlooks, and of course Access. What VBA can do in Microsoft word, maybe is what that you want to know. VBA in Microsoft Word is known as Macro. Macro is like a word command. There are a lot of word command in Microsoft word such as Copy, Paste, Align Left, Align Right, Align Center and mores. A Macro contains VBA code that performs the specific task in Microsoft Word.
Macro is very special. All Word Command can do, it also can do. So what is the special of Word VBA and why do users need to use Macro. It is that a Macro can tie or bind many procedures of VBA code to a single Macro or a single button. This means that Word command can only perform a task one times while Macro performs many tasks one times. For instance a Word Command can copy the text but a Macro can copy and Paste the text. So the Macro's benefits are to improve the work of user with Microsoft Word, performs automatically task, and perform many tasks at the same time in Microsoft Word.
Some Example of VBA command
VBA Code: Copy and Paste all text in Word
Sub CopyAndPaste()
Selection.HomeKey 'go to first page of the document
Selection.WholeStory 'put the selection to all text
Selection.Select 'Select all the text'
Selection.Copy 'copy the text
Selection.PasteAndFormat 'paste the text to Word Document
End Sub
VBA Code: Binding procedures to a single Macro
Sub Main()
dim a as integer
a= Multiply(2,3)
PrintResult(a)
End Sub
Function Multiply(a as integer, b as integer)
Multiply=a*b
End Function
SubPrintResult(result as String)
debug.print result
End Sub
In this example, the first function called Main, it bind the other two function Multiplyand PrintResult. When we run the Macro Main the two procedures Multiplyand PrintResult are called to perform its tasks.