Category Archives: VBA for Word

No Thumbnail

Word VBA, Delete Empty Rows From Tables

One of the things a lot of word users encounter while working in word, are empty rows in tables. In this article I will provide a code that will delete all empty rows from all tables in the document. – Delete Empty Rows From Single Table: In

No Thumbnail

VBA, Write Excel Values to Word Document

In this article I will explain how you can write data from an excel workbook to a word document. The first step in doing this is to determine where the code is going to be written. There are 3 options: In the excel workbook In the

No Thumbnail

Word VBA, Delete Equations

In order to delete an equation using VBA for word, you will have to know its index. Equations are referenced through the OMaths collection. In the example the below the first equation in the document is removed along with all its text: OMaths.Item(1).Range.Select Selection.Delete Before: After:

No Thumbnail

Word VBA, Get Current Line Number

Using the code below you can get the current line the cursor is on: Sub main() Dim intCurrentLine As Integer intCurrentLine = _     Selection.Range.Information(wdFirstCharacterLineNumber) MsgBox (intCurrentLine) End Sub Note: The current page number is relative to the page. That means the line number

No Thumbnail

VBA, Word Table Insert/Remove Rows/Columns

In this article I will explain how you can add and delete rows and columns from tables in a word document using VBA. Every word document has a Tables collection The first step in working with a table in VBA for word is to determine the

No Thumbnail

Word VBA, Get Current Page Number

Using the code below you can get the current page number the cursor is on: Sub main() Dim intCurrentLine As Integer intCurrentLine = _     Selection.Range.Information(wdActiveEndPageNumber) MsgBox (intCurrentLine) End Sub In the figure below the cursor is located at page 10: Result after running the code: See also:

No Thumbnail

VBA, Automating Word From Excel

There are 2 methods for initiating a word application from excel using VBA: Early Binding Late Binding Basically in early binding we define what object we are creating reference to before program execution. In late binding, the program won’t know what we are creating reference to

No Thumbnail

Word VBA, Move Cursor to End of Line

Using the code snippet below you can move the cursor to the end of the Line: Selection.EndKey Unit:=wdLine Assume the cursor is somewhere in the middle of a line: Result after using the code above:   See also: Word VBA, Move Cursor to Start of Document Word

No Thumbnail

Word VBA, Move Cursor to End of Document

Using the code snippet below you can move the cursor to the end of the document: Selection.EndKey Unit:=wdStory Assume the cursor is somewhere in the middle of page 1: Result after using the code above: See also: Word VBA, Move Cursor to Start of Document Word VBA,

No Thumbnail

Word VBA, Print as PDF

In this article I will explain how you can use VBA for word to save a document as a pdf. – Basics: The basic code for saving a word document as a pdf can be seen below: ActiveDocument.ExportAsFixedFormat OutputFileName:= _     “D:StuffBusinessTempPDFName.pdf”, _