Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2006

Tip: Looking for answers? Try searching our database.

Adding text before a bookmark

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Stephen English - 17 Oct 2006 08:46 GMT
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
Jean-Guy Marcil - 17 Oct 2006 12:25 GMT
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).
[quoted text clipped - 10 lines]
> 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.

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Stephen English - 18 Oct 2006 02:39 GMT
Hi Jean-Guy
Thank you so much for the informative and educational response.  I
understand ranges much better.
Kind regards
Stephen

> Stephen English was telling us:
> Stephen English nous racontait que :
[quoted text clipped - 61 lines]
> 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.

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.