Inserting tables before a bookmark

S

Stephen English

Hi
I am having great trouble controlling this.
I am tring to indert a heading before a bookmark (before so that I can
select it again and insert more stuff) and then insert a table for each
record in a recordset and I keep ending up with the table inside the table or
after the bookmark.

Are there some basic rules / methods for moving out of a table please. I
know this looks very messy and this is why I am posting this question. I
have read Cindy Meister's Introduction to Automating Tables.

All suggestions welcome!

Set rngWord = docMaster.Bookmarks("Start").Range
rngWord.Move wdCharacter, -2
rngWord.Collapse
With rngWord
.InsertAfter UCase(rs!Name) & vbCrLf
.Style = "Heading 2"
End With
rngWord.Collapse wdCollapseEnd

Set rngWord = appWord.Selection.Range
rngWord.Move wdCharacter, -1

Do Until rsOfficers.EOF
docMaster.Tables.Add rngWord, 7, 4
' add data to cells from recordset rsofficers
rngWord.Collapse wdCollapseEnd
rngWord.Move wdParagraph, 35 ' 1 for each cell + 1 for each row
Set rngWord = docMaster.Bookmarks("Start").Range
rngWord.InsertBefore vbCrLf
rngWord.Move wdCharacter, -1
rngWord.InsertBefore vbCrLf
rngWord.Collapse wdCollapseEnd
Loop
 
H

Helmut Weber

Hi Stephen,

stay away from the selection.
Yes, easier said than done.
It's about five years I was told so.

Have a look at this one,
which you may adapt to your needs.
I couldn't find a way without deleting a bookmark
and restoring it. But this is common practise anyway,
when filling including bookmarks with text.

As it happens, the code works here and now
on any kind of bookmark.

I didn't care about the formatting.
I'm hoping, you didn't expect a ready to use solution

Sub InsertBeforeBookmark()
Dim rTmp1 As Range
Dim rtmp2 As Range
Dim lTmp As Long
Dim lTmpr As Long
sTmp = "xxxxxxxxxxxx" & vbCr

For lTmp = 1 To 3
With ActiveDocument.Bookmarks("Start")
lTmpr = Len(.Range)
Set rTmp1 = .Range
.Delete
End With

With rTmp1
.InsertBefore sTmp
.start = .start + Len(sTmp)
.Bookmarks.Add "Start"
End With

With ActiveDocument.Bookmarks("Start")
Set rTmp1 = .Range
.Delete
End With

Set rtmp2 = rTmp1
With rtmp2
.Collapse
.Tables.Add rtmp2, 4, 4
' beware of linebreaks by the newsreader, rewrite it this
.start =
ActiveDocument.Tables(ActiveDocument.Tables.Count).Range.Characters.Last.Next.start
.Select ' for testing remove later
.End = .start + lTmpr
Stop ' for testing remove later
.Select ' for testing remove later
Stop ' for testing remove later
.Bookmarks.Add "Start"
End With
Next
End Sub

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
F

Fred

Helmut pardon my ignorance by I receive errors on this test at sTmp first
because it is not defined so I did and then because I did? I had one
bookmark in document" and was expecting it to put XXXXXX etc before the
bookmark(s) - did I miss the point?

Have a look at this one, which you may adapt to your needs. I couldn't find
a 'way without deleting a bookmark and restoring it. But this is common
practise 'anyway, when filling including bookmarks with text.
'As it happens, the code works here and now on any kind of bookmark.
'I didn't care about the formatting. I'm hoping, you didn't expect a ready
to use 'solution

Sub InsertBeforeBookmark()
Dim rTmp1 As Range
Dim rtmp2 As Range
Dim sTmp As Range
Dim lTmp As Long
Dim lTmpr As Long

sTmp = "XXXXXXXXXXXXX" & vbCr 'Runtime error 91 object
variable or block variable not set???

For lTmp = 1 To 3
With ActiveDocument.Bookmarks("Start")
lTmpr = Len(.Range)
Set rTmp1 = .Range
.Delete
End With

With rTmp1
.InsertBefore sTmp
.Start = .Start + Len(sTmp)
.Bookmarks.Add "Start"
End With

With ActiveDocument.Bookmarks("Start")
Set rTmp1 = .Range
.Delete
End With

Set rtmp2 = rTmp1
With rtmp2
.Collapse
.Tables.Add rtmp2, 4, 4
' beware of linebreaks by the newsreader, rewrite it this
.Start =
ActiveDocument.Tables(ActiveDocument.Tables.Count).Range.Characters.Last.Next.Start
.Select ' for testing remove later
.End = .Start + lTmpr
Stop ' for testing remove later
.Select ' for testing remove later
Stop ' for testing remove later
.Bookmarks.Add "Start"
End With
Next
End Sub
 
H

Helmut Weber

Hi Stephen,

add

Dim sTmp as String

Things like that happen,
if one comments out
' option explicit
for whatever reason,
and forgets to enable it again.
 
S

Stephen English

Hi Helmut
THank you for taking the time to give me such a detailed response.
I am writing a database for Rotary when I have time to produce a District
Directory.
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