![]() |
|
#1
|
|||
|
|||
![]()
I would lile to read / write to a version history table in a word 2010 document. I can locate the table with a bookmark, but can't see how I access the individual cells so I can write to or read from the table.
Code to locate table with a bookmark Dim r as Range If ActiveDocument.Bookmarks.Exists("Document_Revision _Table") Then Set r = ActiveDocument.Bookmarks("Document_Revision_Table" ).Range Selection.GoTo What:=wdGoToBookmark, Name:="Document_Revision_Table" Else MsgBox "Missing bookmark" End If As you can tell from this question I am a novice with vb so any explanatory text would be useful. Much appreciated. |
#2
|
||||
|
||||
![]()
You can read from / write to the table as follows:
Code:
Sub Demo() Dim Rng As Range, BkMk as String BkMk = "Document_Revision_Table" With ActiveDocument If .Bookmarks.Exists(BkMk) Then If .Bookmarks(BkMk).Range.Tables.Count > 0 Then With .Bookmarks(BkMk).Range.Tables(1) 'do something, eg: .Cell(1, 1).Range.Text = "Gothca!" Set Rng = .Cell(1, 1).Range Rng.End = Rng.End - 1 MsgBox Rng.Text End With Else MsgBox "Missing table" End If Else MsgBox "Missing bookmark: " & BkMk End If End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] Last edited by macropod; 03-05-2012 at 04:03 PM. Reason: Code fix & enhancement |
#3
|
|||
|
|||
![]()
Thanks for the reply.
When I try running this code I get a run-time error 5941 “The requested member of the collection does not exist.” At the line If .Bookmarks("Document_Revision _Table").Range.Tables.Count > 0 Then I believe this is due to Range.Tables not being part of the collection for bookmarks. ActiveDocument contains “Bookmarks” “Bookmark” contains “Range” not Bookmarks. Or is there something wrong in my configuration? Note a typo in the bookmark name – there is no space. |
#4
|
||||
|
||||
![]()
There was an error in the code, in that I'd copied the first instance of your bookmark name (Document_Revision _Table) without checking its validity. If you look at the bookmark name, you'll see a space in it. Boomark names can't have spaces. I've fixed the code and enhanced it slightly.
It's also worth noting that, for the code to work, you don't need to bookmark the whole table - a bookmark anywhere within it will do. I'd still be inclined to bookmark at least an end-of-row marker rather than something in one of the cells, though (especially not the cell you want to update).
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
![]()
Thanks for your help
|
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
acheo | Word | 1 | 11-24-2011 05:24 PM |
Error: The file may be corrupted, located on a server that is not responding, or read | naeemeh | Excel | 2 | 11-20-2011 03:38 AM |
![]() |
rockwellsba | Word VBA | 2 | 05-31-2011 01:07 AM |
Bookmark Classification | DrDoom | Word | 0 | 06-29-2010 07:08 AM |
Bookmark to another document | spqr | Word | 3 | 06-26-2009 04:51 AM |