wordsite.com

   
 


Frequently Asked Questions about Macros

Introduction to VBA Programming

Programming fundamentals

Returning information

Working with tables

Miscellaneous routines

New Routines 1999-2000

Microsoft is a registered trademark of Microsoft Corporation
 
 

   


Returning Information

Click a topic:

Determine if the insertion point is located at the end of a document.

Detect whether a table cell is empty.

Determine the page number at the current cursor position.

Determine the position of the cursor on the page in points (72 pts. = 1 inch).

Determine the index number of the current paragraph.

Determine the index number of the current table.

Determine the number of pages in a document, using a vba command.

Detect if the first character in a selection is alphanumeric.

Determine whether the selection is at the start of a paragraph.

Determine if the insertion point is located at the end of a document. <Top of Page>

Solution:
If Selection.Type = wdSelectionIP and Selection.End = ActiveDocument.Content.End - 1 Then atEnd = True

Detect whether a table cell is empty. <Top of Page>

Solution:
Use the range object to detect empty cells based on the idea that an empty cell consists of a paragraph mark followed by Chr(7).

Code:
This code is provided for illustrative purposes only and is not warranted to be suitable for any particular business purpose. The code may be freely copied for any lawful business purpose.
Sub CheckTableCells()
For Each oRow In Selection.Tables(1).Rows
For Each oCell In oRow.Cells
If oCell.Range.Text = Chr(13) & Chr(7) Then
MsgBox oCell.RowIndex & " " & oCell.ColumnIndex & " is empty."
End If
Next oCell
Next oRow
End Sub

Here's some code that is similar, but which selects each cell before
announcing whether it is empty.

Sub CheckTableCells()
For Each oRow In Selection.Tables(1).Rows
For Each oCell In oRow.Cells
oCell.Select
If Selection.Text = Chr(13) & Chr(7) Then
MsgBox oCell.RowIndex & " " & oCell.ColumnIndex & " is empty."
End If
Next oCell
Next oRow
End Sub

Determine the page number at the current cursor position. <Top of Page>

Solution:
Selection.Information (wdActiveEndPageNumber)

Determine the position of the cursor on the page in points (72 pts. = 1 inch). <Top of Page>

Solution:
x = Selection.Information(wdHorizontalPositionRelativeToPage)
y = Selection.Information(wdVerticalPositionRelativeToPage)

Determine the index number of the current paragraph. <Top of Page>

Solution:
This code is provided for illustrative purposes only and is not warranted to be suitable for any particular business purpose. The code may be freely copied for any lawful business purpose.
For i = 1 To ActiveDocument.Paragraphs.Count
If Selection.Characters(1).InRange(ActiveDocument.Paragraphs(i).Range) Then
ParagraphIndex = i
Exit For
End If
Next I
MsgBox ParagraphIndex

Determine the index number of the current table. <Top of Page>

Solution:
This code is provided for illustrative purposes only and is not warranted to be suitable for any particular business purpose. The code may be freely copied for any lawful business purpose.
For i = 1 To ActiveDocument.Tables.Count
If Selection.Tables(1).Range.InRange(ActiveDocument.Tables(i).Range) Then
TableIndex = i
Exit For
End If
Next I
MsgBox TableIndex

Determine the number of pages in a document, using a vba command. <Top of Page>

Solution #1:
Selection.Information(NumberOfPagesInDocument)
Solution #2:
ActiveDocument.BuiltInDocumentProperties("Number of Pages")
Solution #3:
ActiveDocument.Content.ComputeStatistics(wdStatisticPages)

Detect if the first character in a selection is alphanumeric. <Top of Page>

Solution:
If Selection.Characters(1) Like "[a-zA-Z0-9]" Then MsgBox "Alphanumeric"
If Selection.Characters(1) Like "[!a-zA-Z0-9]" Then MsgBox "Nonalphanumeric"

Determine whether the selection is at the start of a paragraph. <Top of Page>

Solution:
This code is provided for illustrative purposes only and is not warranted to be suitable for any particular business purpose. The code may be freely copied for any lawful business purpose.
If Selection.Start = Selection.Paragraphs(1).Range.Start Then
MsgBox "At start of a paragraph"
Else
MsgBox "Not at start of a paragraph"
End If