Consulting

Results 1 to 3 of 3

Thread: Solved: Macro to extract Word Info works in Word but NOT Excel

  1. #1
    VBAX Newbie
    Joined
    Dec 2008
    Posts
    2
    Location

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

    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

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

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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:
    [VBA]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[/VBA]

  3. #3
    VBAX Newbie
    Joined
    Dec 2008
    Posts
    2
    Location

    Smile I needed qualifiers to make it work :)

    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
    Last edited by wayneh; 12-04-2008 at 01:18 PM. Reason: Solved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •