Adding text before a bookmark

S

Stephen English

I am iterating though one document (docIndex) and want to keep adding tables
before a bookmark in another document (docAgenda).
Every time I want to add a table to docAgenda, I access
docAgenda.Bookmarks("Start1").Select
I then want to add tables before the bookmark so that next time the bookmark
is at the end of the previously added table.

I have tried various ways of doing it but they seem harder than they should
be and they don't always seem to give me the control I want (such as
adjoining tables get merged)
Selection.Characters.Last.Previous.MoveStart
Selection.InsertAfter vbCrLf & vbCrLf
Selection.Move wdCharacter, -2
Set tblAgenda = Selection.Range.Tables.Add(Selection.Range, 1, 2)

Does anybody have a better way of doing this please?
Thanks
Stephen
 
J

Jean-Guy Marcil

Stephen English was telling us:
Stephen English nous racontait que :
I am iterating though one document (docIndex) and want to keep adding
tables before a bookmark in another document (docAgenda).
Every time I want to add a table to docAgenda, I access
docAgenda.Bookmarks("Start1").Select
I then want to add tables before the bookmark so that next time the
bookmark is at the end of the previously added table.

I have tried various ways of doing it but they seem harder than they
should be and they don't always seem to give me the control I want
(such as adjoining tables get merged)
Selection.Characters.Last.Previous.MoveStart
Selection.InsertAfter vbCrLf & vbCrLf
Selection.Move wdCharacter, -2
Set tblAgenda = Selection.Range.Tables.Add(Selection.Range, 1, 2)

Try something like this to get a table sandwich between two ¶'s (So it will
not join with other tables around it)::

'_______________________________________
Dim docAgenda As Document
Dim rgeAgenda As Range
Dim tblAgenda As Table

Set docAgenda = ActiveDocument
Set rgeAgenda = docAgenda.Bookmarks("Start1").Range

With rgeAgenda
.MoveEnd wdCharacter, -1
Set tblAgenda = .Tables.Add(rgeAgenda, 1, 2)
With tblAgenda
.Cell(1, 1).Range.Text = "My first heading"
'etc
End With
.InsertParagraphAfter
End With
'_______________________________________

If you wan a table without the extra ¶'s. try this:

'_______________________________________
Dim docAgenda As Document
Dim rgeAgenda As Range
Dim tblAgenda As Table

Set docAgenda = ActiveDocument
Set rgeAgenda = docAgenda.Bookmarks("Start1").Range

With rgeAgenda
.MoveEnd wdCharacter, -1
Set tblAgenda = .Tables.Add(rgeAgenda, 1, 2)
With tblAgenda
.Cell(1, 1).Range.Text = "My first heading"
'etc
End With
.End = tblAgenda.Range.End
.Collapse wdCollapseEnd
.Delete
End With
'_______________________________________

The trick is not to use the Sselection object, but rather a Range object. It
is faster, more reliable and does not move the user selection.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
S

Stephen English

Hi Jean-Guy
Thank you so much for the informative and educational response. I
understand ranges much better.
Kind regards
Stephen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top