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 / April 2007

Tip: Looking for answers? Try searching our database.

Inserting tables before a bookmark

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Stephen English - 19 Apr 2007 15:26 GMT
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
Signature

Regards
Stephen English

Helmut Weber - 19 Apr 2007 16:54 GMT
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

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Fred - 20 Apr 2007 08:03 GMT
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

> Hi Stephen,
>
[quoted text clipped - 57 lines]
>
> HTH
Helmut Weber - 20 Apr 2007 08:15 GMT
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.

--
Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000 (german versions)
Stephen English - 23 Apr 2007 14:00 GMT
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
Signature

Regards
Stephen English

> Hi Stephen,
>
[quoted text clipped - 13 lines]
> "red.sys" & chr(64) & "t-online.de"
> Word 2002, Windows 2000 (german versions)
 
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.