inserting section breaks from Access in code...

P

Piet Linden

Sorry for the lame subject... Anyway... on to the problem!

I am working on some code that is executing from within Access.

I am trying to insert a chunk of text (which works fine) and then
after each chunk, insert a continuous section break. The section
breaks get inserted, but they're all mysteriously at the top of the
document instead of between the sections of text! What am I doing
wrong?

Here's the code... (don't laugh, I'm most definitely not a good Word
programmer!)

Private Sub OpenQueries()
<SNIP>

' Open a new document to dump data into... change to a template if
necessary
objWordApp.Documents.Add
Set objWordDoc = objWordApp.ActiveDocument


'---THE SECTION BREAK INSERTS WORK, BUT THEY APPEAR AT THE TOP OF THE
DOCUMENT INSTEAD OF BETWEEN THE CHUNKS OF TEXT.... .Content
or .Range does the same thing... what gives?

If varItem = "PopulationSize" Then
'FAILS
varData = rstAny.GetString()
.Range.InsertAfter "Population," & varData
.Range.InsertParagraphAfter
'
.Content.InsertBreak Type:=wdSectionBreakContinuous
Else
varData = rstAny.GetString()
.Range.InsertAfter varData
.Range.InsertParagraphAfter
.Content.InsertBreak Type:=wdSectionBreakContinuous
End If

End With

rstAny.Close

Next varItem

Okay, so what's the proper way to Insert a section break after each
inserted chunk of text?
The GetString stuff is returning a delimited chunk of text that writes
just fine into Word... I just can't get the section breaks to insert
in the right place. They all mysteriously end up at the top of the
document! What gives?

Thanks!
Pieter
 
P

Peter Jamieson

I would try something more like:

Di r as Word.Range
With objWordDoc
Set r = .Content
r.Collapse Direction:=wdCollapseEnd
r.InsertAfter "some text"
r.Collapse Direction:=wdCollapseEnd
r.InsertParagraph
r.Collapse Direction:=wdCollapseEnd
r.InsertBreak wdSectionBreakContinuous
Set r = Nothing
End With

The thing about the range is that it extends as you do stuff to it, but
if you do something like

r.InsertBreak Type:=wdSectionBreakContinuous

the break will be inserted at the beginning of the range.

..content is always the complete content of the document, so doing a

..content.InsertBreak Type:=wdSectionBreakContinuous

will insert the break at the beginning of the document.
Here's the code... (don't laugh, I'm most definitely not a good Word
programmer!)

Nor am I. FWIW, I find the way Word ranges work non-intuitive and
unhelpful, but they aren't going to change any time soon so the only
thing for it is to discover how they work and work around their
limitations and/or find a standard way of working that works for the
stuff you need, then stick to it.


Peter Jamieson

http://tips.pjmsn.me.uk
 

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