Microsoft Word Visual Basic (VBA) Macro Examples and Tutorials

Use of Bookmarks in Visual Basic

Real-world VBA macros for Microsoft Word automation, including code for bookmarks, data extraction from documents, and pattern matching. In these pages you'll find examples of how to use Visual Basic macros. These are practical examples that have worked for me. I have based these examples on my own use of Visual Basic.

Example: Simple Bookmark Insertion Macro

Sub InsertBookmark()
    Selection.Bookmarks.Add Name:="MyBookmark", Range:=Selection.Range
    MsgBox "Bookmark added!"
End Sub
Tutorial on Bookmarks →


Extract Data Macro

The next few pages contain versions of a macro under development. The idea is to give some insight into the visual basic development process, and the use of different VB commands. Basically, this macro extracts data from different documents, processes the data, and stores it in a single file.

In this version of the macro Extract Data Write Version the "Write" command is used to write data to a file.

Extract Data Write Version Tutorial →

Example Macro: Extract Data from Log File (Makro1 / Extract Data Write Version)

This VBA macro extracts electronic transaction data (account number, liters, amount) from a Word document log file and writes it to a target file with the "Write" command.

Type Electronic_transaction
    Account_number_Sort_code As String
    Transaction_liters As Single
    Transaction_amount As Currency
End Type

Sub Makro1()
    ' Extract customer data from log file
    Dim Electronic_Transaction_Record As Electronic_transaction
    Dim Transaction_amount As Currency
    Dim Transaction_liters As Single
    Dim Temp_Transaction_Amount As Currency

    ' Initialization and processing logic...
    ' (Full code processes the document, finds transactions, extracts data, and writes to file)

    ' ... [code continues as on the page]

End Sub

Note: This is a partial preview. The full macro includes detailed searching, extraction, and file writing logic.


In this version of the macro Extract Data Put Version the "Put" command is used to write data to a file.

Extract Data Put Version Tutorial →


Regular Expression  - "/" - Backslash - Quote Next Char

A simple implementation of the regular expression "/" Backslash in Visual Basic. This regular expression is also known as "Quote Next Char."


Wildcard* - matches zero or more characters

A simple implementation of the Wildcard (asterisk) in Visual Basic."

Examples:

Gegensatz

a*tz matches atz

g*tz matches gensatz

G*tz matches Gegensatz

Wildcard* is very useful in parsing text for machine translation. When German is the source text, for example, it can be used to link two parts of separable verbs that can have any number of words between them, in order to translate the verb correctly.


← Back to Visual Basic Macros Main Page ← Back to Homepage







.