P
Patrick Finucane
Bear with me, there is a reason for what I need to do. I have a
document template with mail merge fields. This form will be mail merged
from Access and saved. A situation may come up where I need to grab the
data between the salutation and the Sincerely and paste it into the new
document. This is my first attempt at working with Word VBA...I'm
hoping you can spot what I need to do.
Ex of the document:
Joe Blow
123 Somewhere St
Anywhere USA 12345<BodyTextStart Bookmark>
blah blah blah
blah blah blah
<BodyTextEnd Bookmark>Sincerely,
Jane Doe
The lines of blah, blah, blah are the body text. I put in a couple of
bulleted items and bolded a couple of workds and ran the tollowing
code. It WORKS!!!!...except the bolding gets lost and and bullets are
gone...just the text. Can you help out?
Sub testDocument()
On Error GoTo ErrorHandler
Dim lngStart As Long
Dim lngEnd As Long
Dim aRange As Range
'bookmark where the body of the letter will start and end
lngStart = ActiveDocument.Bookmarks("BodyTextStart").Start
lngEnd = ActiveDocument.Bookmarks("BodyTextEnd").Start - 1
'define the range
Set aRange = ActiveDocument.Range(Start:=lngStart, End:=lngEnd)
'I set a range to BodyTextEnd and did an edit paste but
'it overwrote the Sincerely stuff. I need to insert, not paste over
'aRange.Select
'aRange.Copy
'aRange.Paste
'the following line does not work or compile but from what
'I read, PasteSpecial does what I want this routine to do (I think).
'aRange.InsertAfter aRange.PasteSpecial DataType:=wdPasteText
'these are close but no cigar..the formatting of bold and bullets
not there
'aRange.InsertAfter aRange.FormattedText.Duplicate
'or this
'aRange.InsertAfter aRange.FormattedText
MsgBox "Done"
Exit_Test:
Exit Sub
ErrorHandler: ' Error-handling routine.
If Err.Number = 5941 Then
MsgBox "One of the bookmarks does not exit"
Else
MsgBox Err.Description
End If
Resume Exit_Test
End Sub
document template with mail merge fields. This form will be mail merged
from Access and saved. A situation may come up where I need to grab the
data between the salutation and the Sincerely and paste it into the new
document. This is my first attempt at working with Word VBA...I'm
hoping you can spot what I need to do.
Ex of the document:
Joe Blow
123 Somewhere St
Anywhere USA 12345<BodyTextStart Bookmark>
blah blah blah
blah blah blah
<BodyTextEnd Bookmark>Sincerely,
Jane Doe
The lines of blah, blah, blah are the body text. I put in a couple of
bulleted items and bolded a couple of workds and ran the tollowing
code. It WORKS!!!!...except the bolding gets lost and and bullets are
gone...just the text. Can you help out?
Sub testDocument()
On Error GoTo ErrorHandler
Dim lngStart As Long
Dim lngEnd As Long
Dim aRange As Range
'bookmark where the body of the letter will start and end
lngStart = ActiveDocument.Bookmarks("BodyTextStart").Start
lngEnd = ActiveDocument.Bookmarks("BodyTextEnd").Start - 1
'define the range
Set aRange = ActiveDocument.Range(Start:=lngStart, End:=lngEnd)
'I set a range to BodyTextEnd and did an edit paste but
'it overwrote the Sincerely stuff. I need to insert, not paste over
'aRange.Select
'aRange.Copy
'aRange.Paste
'the following line does not work or compile but from what
'I read, PasteSpecial does what I want this routine to do (I think).
'aRange.InsertAfter aRange.PasteSpecial DataType:=wdPasteText
'these are close but no cigar..the formatting of bold and bullets
not there
'aRange.InsertAfter aRange.FormattedText.Duplicate
'or this
'aRange.InsertAfter aRange.FormattedText
MsgBox "Done"
Exit_Test:
Exit Sub
ErrorHandler: ' Error-handling routine.
If Err.Number = 5941 Then
MsgBox "One of the bookmarks does not exit"
Else
MsgBox Err.Description
End If
Resume Exit_Test
End Sub