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.
Sub InsertBookmark()
Selection.Bookmarks.Add Name:="MyBookmark", Range:=Selection.Range
MsgBox "Bookmark added!"
End Sub
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.
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.
A simple implementation of the regular expression "/" Backslash in Visual Basic. This regular expression is also known as "Quote Next Char."
A simple implementation of the Wildcard (asterisk) in Visual Basic."
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.
.