PDA

View Full Version : Solved: Macro to extract Word Info works in Word but NOT Excel



wayneh
12-03-2008, 08:48 PM
Hi
The following macro to find which table/cell the cursor is currently in works fine in Word 2003 but not in Excel 2003 (I have referenced Word 11 library)
Get runtime error 438 Object does not support this property or method on line beginning "TableIndex = ...."

Any help gratefully received :)
Thanks
Wayne
-------------------------------------------------------------

Sub WhereAreWe()

Dim oDoc As Word.Document
Dim oWord As Word.Application

Set oApp = GetObject(, "Word.Application")

Set oDoc = oApp.ActiveDocument

' Which table are we in ?
TableIndex = oDoc.Range(0, Selection.Tables(1).Range.End).Tables.Count

' Which cell ?
CurrentRow = Selection.Information(wdStartOfRangeRowNumber)
CurrentColumn = Selection.Information(wdStartOfRangeColumnNumber)

MsgBox "Table Number: " & CStr(TableIndex) & vbCrLf _
& "Row Number: " & CStr(CurrentRow) & vbCrLf _
& "Column Number: " & CStr(CurrentColumn)

End Sub

----------------------------------------------------------------

Kenneth Hobs
12-03-2008, 09:55 PM
Welcome to the forum!

You assume 2 things which cause errors. However, if you are sure that the focus is in an MSWord table when you play this, you will be fine.

Try:
Sub WhereAreWe()

Dim oDoc As Word.Document
Dim oWord As Word.Application
Dim oApp As Object
Dim TableIndex As Integer
Dim CurrentRow As Long
Dim CurrentColumn As Integer

Set oApp = GetObject(, "Word.Application")

Set oDoc = oApp.ActiveDocument

' Which table are we in ?
TableIndex = oDoc.Range(0, oDoc.Tables(1).Range.End).Tables.Count

' Which cell ?
CurrentRow = oDoc.Range.Information(wdStartOfRangeRowNumber)
CurrentColumn = oDoc.Range.Information(wdStartOfRangeColumnNumber)

MsgBox "Table Number: " & CStr(TableIndex) & vbCrLf _
& "Row Number: " & CStr(CurrentRow) & vbCrLf _
& "Column Number: " & CStr(CurrentColumn)

End Sub

wayneh
12-04-2008, 01:16 PM
Thanks for the response Kenneth
Finally got it to work - beacuse I was running from Excel I needed to qualify the Selection method as being for Word - not Excel
Working code posted below
Thanks again for your reponse
Wayne

Sub WhereAreWe()

' Works in Excel

Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oRange As Word.Range
Dim oSelection As Word.Selection
Dim TableIndex, CurrentRow, CurrentColumn, NoOfTables As Integer

Set oWord = GetObject(, "Word.Application")

Set oDoc = oWord.ActiveDocument

NoOfTables = oDoc.Tables.Count

' Which table are we in ?
' Range from Table(1) to position of Cursor
Set oRange = oDoc.Range(0, oWord.Selection.Tables(1).Range.End)

' Count number of tables in range (= index of table that Cursor is in :)
TableIndex = oRange.Tables.Count

' Which cell in this table ?
CurrentRow = oWord.Selection.Information(wdStartOfRangeRowNumber)
CurrentColumn = oWord.Selection.Information(wdStartOfRangeColumnNumber)

MsgBox "Table Number: " & CStr(TableIndex) & vbCrLf _
& "Row Number: " & CStr(CurrentRow) & vbCrLf _
& "Column Number: " & CStr(CurrentColumn)

End Sub