Word - W2003: find cursor location in table coordinates (table/row/column
Asked By ker_01
19-Mar-10 04:36 PM
I have been asked to extract data from an existing form that I did not set up,
and the format is less than ideal. To minimize my cycles of testing, it would
be much easier to place my cursor in a sample document, and then debug.print
the cursor location as table coordinates so I can hardcode the 'cells' that I
need to extract. There are about 15 tables total, and I will have to extract
content from most of them.
I played around with the range.parent object but could not figure out how to
return the table info.
I also googled, but no joy.
Any suggestions or snippets would be greatly appreciated.
Thanks,
Keith
Selection.Cells.NestingLevel
(1)
Selection.Cells.Count
(1)
ActiveDocument.Range
(1)
Selection.Tables
(1)
StringpTableNumber
(1)
Tables.Count
(1)
PTableNumber
(1)
LngNestLvl
(1)
ker_01 replied to ker_01
I kept searching and found a partial answer here:
http://www.vbaexpress.com/kb/getarticle.php?kb_id=867
but that still does not get me the table number. Can anyone provide the
object model syntax to capture the table count as well?
Thank you!
Keith
Greg Maxey replied to ker_01
I have not used this in a while, but I think it might help you:
http://gregmaxey.mvps.org/Table_Cell_Data.htm
Greg Maxey replied to ker_01
Dim pTableNumber As String
pTableNumber = "The selection is in table: " & _
ActiveDocument.Range(0,
Selection.Tables(1).Range.End).Tables.Count
MsgBox pTableNumber & ". The selection covers " &
Selection.Cells.Count & " cells, from Cell(" & _
ker_01 replied to Greg Maxey
That definitely gives me the table number, and now I realize why the source
form looks so 'wonky' - it has tables embedded in tables. So, When I am in
into the first cell of the embedded table, I also get "Table 3 (1,1)".
Is there a preferred method for identifying embedded tables so that I can
treat each one independently? I have some cells in parent tables that appear
to have more than one child (embedded) table, even within just the one cell.
Thank you!
Keith
ker_01 replied to ker_01
I am halfway there; I just had to find out that the object model uses "nested"
instead of "embedded" to describe these tables. So now I added:
lngNestLvl = Selection.Cells.NestingLevel
Which accurately gives me the /level/ of nesting, but does not give me the
table number of the nested table.
In other words, I create a 1-cell table, and within that cell I add two
different nested tables. I run my code in cell 1 of each nested table, and
they both tell me that I am in table 1 (overall parent table), nested at
level 2, and in cell 1,1 (which seems to refer to the child table, not the
parent table).
If there is more than one nested table in a single cell, how do I tell which
one I am in?
Thank you!
Keith
Thanks in advance for any information. The macro I = 92ve = created is below. Sub TEST() Selection.Tables(1).Rows(2).Select Selection.SplitTable Selection.MoveDown Unit: = 3DwdLine, Count: = 3D1 Selection.Tables(1).Columns(1).Select Selection.Columns.PreferredWidthType = 3D wdPreferredWidthPoints Selection.Columns.PreferredWidth = 3D InchesToPoints(1 20 Selection.Tables(1).Columns(2).Select Selection.Columns.PreferredWidthType = 3D wdPreferredWidthPoints Selection.Columns.PreferredWidth = 3D InchesToPoints(1) = 20 Selection.Tables(1).Columns(3).Select Selection.Columns.PreferredWidthType = 3D wdPreferredWidthPoints Selection.Columns.PreferredWidth = 3D InchesToPoints(2
As String Dim par As Object Dim i As Integer 'Dim par As Range If ActiveDocument.Range.Paragraphs.Count > 0 Then For Each par In ActiveDocument.Paragraphs MsgBox par Next par End As String Dim par As Object Dim i As Integer 'Dim par As Range If ActiveDocument.Range.Paragraphs.Count > 0 Then For Each par In ActiveDocument.Paragraphs MsgBox par Next par End As String Dim par As Object Dim i As Integer 'Dim par As Range If ActiveDocument.Range.Paragraphs.Count > 0 Then For Each par In ActiveDocument.Paragraphs MsgBox par Next par End
in Office 2003: Selection.ConvertToTable Separator: = wdSeparateByTabs, NumColumns: = 2, _ NumRows: = 39, AutoFitBehavior: = wdAutoFitContent With Selection.Tables(1) .Style = "Tabelraster" .ApplyStyleHeadingRows = True .ApplyStyleLastRow = True .ApplyStyleFirstColumn = True .ApplyStyleLastColumn = True End With I see To anyone else happening to struggle with this, a simple line will do the trick: Selection.Tables(1).AutoFitBehavior (wdAutoFitContent) Don't know why the first mention of Autofit does not take
as in $. $ 'Does not insert a space if a number precedes or follows the period. ActiveDocument.Range.Find.Execute Findtext: = 3D"([A-Za-z].)([A-Za-z])", = _ MatchWildcards: = 3DTrue, Wrap: = 3DwdFindContinue Does not insert a space if a number precedes or follows the = 20 period.<BR> ActiveDocument.Range.Find.Execute = 20 Findtext: = 3D"([A-Za-z].)([A-Za-z])", _<BR> MatchWildcards: = 3DTrue
Add Range: = Selection.Range, NumRows: = 1, NumColumns: = _ 1, DefaultTableBehavior: = wdWord9TableBehavior, AutoFitBehavior: = _ wdAutoFitFixed With Selection.Tables(1) If .Style <> "Table Grid" Then Style = "Table Grid" End If ApplyStyleHeadingRows = True ApplyStyleLastRow = True Courier New" Selection.Font.Size = 11 Selection.TypeText Text: = "CHANGED P / N INFORMATION" Selection.NextField Selection.Tables(1).Columns(1).SetWidth ColumnWidth: = 565, RulerStyle: = _ wdAdjustNone Selection.MoveDown Unit: = wdLine, Count: = 1