How to append a blank paragraph to a range object

A

Ancient Brit

I have two documents - source and target. I need to copy sections from the
source document to the target document, excluding the section break but
including formatted text, tables, embedded graphics, etc. That much I have
working. I ended up using the clipboard for simplicity (even though I know
it's not the best route).

The section breaks are to be excluded in order to preserve existing
headers/footers, margins, and other page information in the target document.
We want to preserve as much as possible existing formatting from the source
docs.

However, in some cases a section in the source document consists of a
numbered heading and a continuous section break on the same line, with NO
pilcrow/paragraph mark between the two.

So when I exclude the section break, I don't get paragraph formatting for
that section.

For a reasonably well-formed source document there is at least a paragraph
mark before each section break, but there appear to be a set of rogue
documents that don't, and I need to cater for them. They could run into the
hundreds. The numbers of sections might also run into the hundreds.

I appear to be able to identify easily enough when a selected range ends in
something other than a carriage return - I check for the last character in
the range being ASCII(13).

What I'm stumped on is how to add in a necessary carriage return at the end
of the range so that the paragraph formatting is carried over to the target
document. Simplistic appending of a CHR$(13) to the range produces a mismatch
error so that's a dead end.

Anyone any ideas? Here's some rough code that I have working so far (for
obscure reasons I need to exclude the first section so the loop begins at 2,
and I've left out a bunch of other irrelevant code for clarity):

Set M_SourceDoc = ActiveDocument 'Target already open; user has opened
Source doc
For M_Loop = 2 To M_SourceDoc.Sections.Count
Set M_InitialRange = M_SourceDoc.Sections(M_Loop).Range
M_InitialRange.SetRange Start:=M_InitialRange.Start,
End:=M_InitialRange.End - 1
M_InitialRange.Copy
Documents(M_TargetDoc).Activate
Selection.Paste
...other bits followed by refocus to M_SourceDoc
Next M_Loop

Grateful for any pointers. I had wondered about possibly inserting a
carriage return into the source document when such sections arise but I'm not
keen to make changes to the source if at all possible.

Best,

Peter
 
L

Lene Fredborg

You could do something like this:

When you have set M_InitialRange, store information about the style of the
last paragraph. After having pasted the copied text, the selection is in that
paragraph in the target document. Now apply the style in question. This
requires that a style with the style name in question be found in the target
document. It you cannot rely on that, you will need to include handling of
this in your code.

The code you provided could then be changed to something like the following:

Dim strStyleLastPara As String

Set M_SourceDoc = ActiveDocument 'Target already open; user has opened
Source doc
For M_Loop = 2 To M_SourceDoc.Sections.Count
Set M_InitialRange = M_SourceDoc.Sections(M_Loop).Range
M_InitialRange.SetRange Start:=M_InitialRange.Start,
End:=M_InitialRange.End – 1

’Store info about style of last paragraph
strStyleLastPara = M_InitialRange.Paragraphs.Last.Style

M_InitialRange.Copy
Documents(M_TargetDoc).Activate
Selection.Paste

'Selection now in last paragraph pasted - apply the style
Selection.Paragraphs(1).Style = strStyleLastPara

...other bits followed by refocus to M_SourceDoc
Next M_Loop

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
A

Ancient Brit

Hi Lene

Thanks for the useful feedback - I had wondered about doing that, but it
seemed a lot of work (and would the character and other styles in use in the
paragraph also be transferred unless I also retrieved their info as well?
This is Word 2003 so...). There's also the issue that the target document has
a custom styleset and does not possess any of the styles in the source
document.

In the end I've bitten the bullet and gone with the simplest approach: after
reducing the range to exclude the section break, I check to see whether the
final character in the reduced range is a carriage return (ASCII 13 or vbCr).
If it isn't then I add one with:

If Right$(M_InitialRange, 1) <> vbCr Then M_InitialRange.InsertAfter vbCr

which inserts one in the source document between the last character and the
section break - and the range extends to incorporate it automatically,
thereby retaining the paragraph formatting.

I get around the problem of having modified the source document by closing
it, abandoning all changes, to eliminate the possibility that the user might
save it if it was left open after processing for comparison. The user will
just have to re-open it manually if they want to check that everything copied
over as required :)

Grateful for the idea, though.

Best,

Peter

--
 
L

Lene Fredborg

I am glad you found a solution that works for you.

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 

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