Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bookmarked Table not within Table? 1

Status
Not open for further replies.

DrSimon

IS-IT--Management
Dec 14, 2001
674
GB
I have bookmarked a number of tables within a Word 2007 document but when within VBA I select such a bookmark and test them I find that Selection.Information(wdWithInTable) is False!
The reason I want to do this is that I need to do some processing on the contents of certain tables and I'm using a common Bookmark prefix to identify them. I can get round it by bookmarking the first cell, but just though it odd.

Any comments?
Simon Rouse
 



Hi,

Why not name the tables or use the tables collection?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Hi Skip
I knew someone would ask something like that!
I'm dealing with 'template' (not real .dot) documents that contain numerous tables and I'm writing code/processes to update the final documents with document variables and custom document properties to make the whole thing more efficient and improve quality. There are certain things that need to be done through the front end and other though the back end. The 'template' must satisfy the customer. As far as I'm aware table names can only be set using VBA code, not via the front end - am I wrong? I need some ID to allow me to go through all the objects and inserting a bookmark with a common prefix seemed the best way.

If you can think of another approach, I'd be happy to hear of it.

Simon
 


Code:
Sub BookmarkTables()
    Dim tb As Table, i As Integer
    
    For Each tb In ThisDocument.Tables
        ThisDocument.Bookmarks.Add Name:="BKMK" & Format(i, "00"), Range:=tb.Range
        i = i + 1
    Next
End Sub

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Nice try, but that's not it. There are only certain table 'types' that I want to interact with and I can set up the those bookmarks from the front end.What I can't understand is when I select a table and give it a bookmark name that when I loop though the bookmarks and look at the Selection.Information property it says that it isn't WithInTable. I can only assume that from a pedantic point of view the bookmark is not actually in the table but is the table!

Simon
 


If your bookmark includes any range outside the table, then selecting the bookmark will NOT indicate wdWithInTable is TRUE, because it is NOT TRUE!

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
OK - here's what happens and it seems that you're right!
From Word front end:
Select a cell in a table
Select Table
Add Bookmark
Then run this code:
Code:
For Each BMark In ActiveDocument.Bookmarks
    BMark.Select
    If Selection.Information(wdWithInTable) = False Then
        MsgBox "Selection is not in a table."
    End If
Next BMark
...and the bookmark created above fails as I described.
However bearing in mind what you said, I then inserted Selection.Tables(1).Select after BMark.Select and ...... it works!

So it would seem that if you bookmark a table that bookmark must be partly outside that table..... or something!

Thanks for the discussion

Simon
 


So it would seem that if you bookmark a table that bookmark must be partly outside that table
Well all that I can say, since I am FAR from a Word expert, that the code I posted , produced bookmarks that, when selected, DID return TRUE using your Information statement.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I'm sure it did, so the front-end bookmark is different. Hey nobody's perfect - not even Microsoft.

Thanks again
Simon
 
If you are using VBA, why are you using Selection at all??? If you are using VBA, who cares if the Selection is in any given table? Why not just leave the Selection where it is and work with the bookmarked tables themselves?


unknown
 
Hi Fumei
I need to inspect and sometimes update the contents of a number of cells in the tables and couldn't work out any other way to do it. Now you mention it -that may well be much better! I'll try again tomorrow and let you know!

Simon
 
Just to expand...
Code:
Dim oTable As Table
Dim i As Long
i = 1
For Each oTable In ActiveDocument.Tables
   ActiveDocument.Bookmarks.Add Name:="Table_" & i, _
      Range:=oTable.Range
   i = i + 1
Next
This is the same as Skip's code. So we end up with, for example, a bookmark named: Table_2.

OK?

Now...
Code:
ActiveDocument.Bookmarks("Table_2").Select
If Selection.Information(wdWithInTable) = False Then
   MsgBox "Selection is not in a table."
Else
   MsgBox "Selection IS in a table."
End If
will return "not in a table". However,
Code:
ActiveDocument.Bookmarks("Table_2").Range.Tables(1).Select
will return "IS in a table".

While I reiterate it is better not using Selection at all, if you must (...and why exactly?), at least select what you actually want to select, i.e. the table.


unknown
 
update the contents of a number of cells in the tables and couldn't work out any other way to do it."

By actually making a table object of the bookmarked table (which by the way can now be moved freely in the document and still retains it identifier - i.e. the bookmark), this can be EASILY done. Assuming again a bookmarked table named "Table_2":
Code:
Dim aTable As Table
Set aTable = ActiveDocument.Bookmarks("Table_2").Range.Tables(1)
now the table, without EVER needing to select anything - can be done actioned, even in a document that is NOT active. Say you have document "Blah.doc" active. You previously opened "Whatever.doc", but it is not active. Whatever.doc has bookmarked tables.
Code:
Dim WhoCares As Document
Dim aTable As Table
Set WhoCares = Documents("Whatever.doc")
Set aTable = WhoCares.Bookmarks("Table_2").Range.Table(1)

aTable.Cell(1,3).Range.Text = ActiveDocument.FormFields("Client").Result
The above takes the value of the formfield "Client" and puts it in the Row1/Column3 cell of the bookmarked table "Table_2" in the document Whatever.doc all without ever selecting anything, or using Activate to change documents.



unknown
 
That's it fumei. I've never really looked at Word objects before and jumped in with what I could find as I needed to do something quickly.

As so often happens here, this forum gives me what I need rather than what I asked for! I still suspect that I've detected a 'feature' that could trap others, but hey that's no longer my problem! I can now move around tables and between documents to my heart's content.

Many thanks and a well deserved star

Simon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top