Insert Page break after last period in copied/pasted text.

J

justagrunt

Hello,
How can I insert a new page break after the last full stop, period in a
pasted text but before the next bookmark. The bookmarks are in white font on
the page, (numbered 1 thru 11 and identified as bookmarks as section1 thru
section11).
I have tried - rem'ed out because it must have inserted after the last
bookmark, so the next page could not be pasted as the bookmarks wern't there,
I guess.

'open the letter
Set destdoc9 = Wordapp.Documents.Open(strInputFileName) 'letter

With destdoc9
..Bookmarks("section9").Range.FormattedText = srcDoc.Range.FormattedText
Rem .Range.InsertBreak Type:=wdPageBreak
..close (wdSaveChanges)
End With
'close the Attachment sheet
srcDoc.close (wdDoNotSaveChanges)
Else
'do nothing
End If
 
H

Helmut Weber

Hi,

I'm assuming, you are talking about including [bookmarks].

If you insert text at an including bookmark,
the bookmark vanishes.

See: http://word.mvps.org/faqs/macrosvba/InsertingTextAtBookmark.htm

So, if you follow the instructions,
the bookmark is there again, but spans the inserted text.

You may now serach the range of the newly created bookmark
for the first period and insert a pagebreak after it.

Still a stab in the dark.

Sub Test4561()
Dim rDcm As Range
Dim rTmp As Range
Set rDcm = ActiveDocument.Range
Set rTmp = rDcm.Bookmarks("Lastname").Range
rTmp.Text = "yyyyyy.yyyyyy"
rTmp.Bookmarks.Add "Lastname", rTmp
With rTmp.Find
.Text = "."
If .Execute Then
rTmp.InsertAfter Chr(12)
End If
End With
End Sub

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

justagrunt

Hi Helmut,
Exactly what I was trying to explain, you did it better.
Yes that is what happens, when the text pastes it becomes the bookmark, it
is the included bookmark.
Thanks for the help - will implement.
 
J

justagrunt

Hi Helut,
Slight technical problem.
If I follow the example and use the following, dim's not shown but code
compiles OK,


If Dir(strCom) = "" Then
MsgBox "File does not exist"
Else
Set srcDoc = Wordapp.Documents.Open(strCom) 'attachment sheet
End If

'open the letter
Set destdoc7 = Wordapp.Documents.Open(strInputFileName) 'letter
Set bmrange = destdoc7.Bookmarks("section7").Range
bmrange.Text = srcDoc.Range.Text
destdoc7.Bookmarks.Add "section7", bmrange
destdoc7.Bookmarks("section7").Range.InsertBreak Type:=wdPageBreak
destdoc7.close (wdSaveChanges)

Rem With destdoc7
Rem.Bookmarks("section7").Range.FormattedText = srcDoc.Range.FormattedText
Rem .close (wdSaveChanges)
Rem End With

'close the Attachment sheet
srcDoc.close (wdDoNotSaveChanges)
Else
'do nothing
End If

I get a section break and no bookmark (section7),no text and enclosing
bookmarks either side.


Changing the formattedtext for text did made no difference.
I've run it thru 4 other sections and get the same result, section breaks,
no text

your welcome advice is appreciated.
 
H

Helmut Weber

Hi Bill,
destdoc7.Bookmarks("section7").Range.InsertBreak

is equivalent to inserting text.
A section break is text, too, just kind of special text.

So you're doing exactly what you avoided beforehand,
defining a bookmarks text anew and thus deleting
the text which was there before *and* the bookmark.


HTH
 

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