Good articles, Doug, especially about InsertAfter and InsertBefore since I've
never used those features.
I was about to write the same thing that Doug wrote about the bookmarks
being deleted in the merge-to document. Otherwise, your syntax to pick up the
bookmark text is correct (with the notation of .text that you affirmed you
are using).
Following is a macro I use for the issue of bookmarks disappearing, but as I
warned earlier, it involves a lot of code and is not really a "merge"
operation, per se. Things to take into account: (1) Don't use the brackets []
literally. (2) This macro assumes that only one DB record is being used at a
time in your document; in other words, that you're not making a list with the
same mergefield being used with multiple records in the same document. (3) I
list some utility macros ("CloseDoc," etc.) after the main one and they are
referenced/used in the main macro. I use these utilities a lot so I just
abbreviate the multiple commands into one macro name.
Sub [YourMacroName]()
Dim strDoc As String
Dim strPath As String
'use variable to pick up doc name
'if it's always the same doc name, use strDoc = "[yourDocName].doc"
'otherwise, if always a different doc name, replace line below with
'strDoc = ActiveDocument.Name
strDoc = "[yourDocName].doc"
strPath = Documents(strDoc).path
'make double sure that desired document is open and active
On Error Resume Next
Documents(strDoc).Activate
On Error GoTo 0
If ActiveDocument.name <> strDoc Then Documents.Open strPath & strDoc
'macro ends if the correct document doesn't open
If ActiveDocument.name <> strDoc Then End
'select text of whole document
With Selection
.WholeStory
.Copy
End With
'open new document and paste into it
NewDoc
With Selection
.Paste
'delete any extra paragraph mark at end
.Delete
'uncomment and insert correct doc name in next command if you want
to save the doc
'ActiveDocument.SaveAs "[newDocName].doc",
FileFormat:=wdFormatDocument
.WholeStory
'unlinks fields which original merge doc created
.Fields.Unlink
End With
DocHome
'do your bookmark operations here
'uncomment the following if you want to go back to your original doc
after all is said and done
'On Error Resume Next
'Documents(strDoc).Activate
'On Error GoTo 0
'If ActiveDocument.name <> strDoc Then Documents.Open strPath & strDoc
End Sub
Sub DocHome()
Selection.HomeKey unit:=wdStory
End Sub
Sub NewDoc()
Documents.Add Template:="Normal", NewTemplate:=False
End Sub
--
Bryan
Doug Robbins - Word MVP said:
Are you sure that the bookmark still exists in the document after you
populate it with the data from Access?
See the article "Working with Bookmarks in VBA" at:
http://www.word.mvps.org/FAQs/MacrosVBA/WorkWithBookmarks.htm
and "Inserting Text at a Bookmark without deleting the Bookmark" at:
http://www.word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm
--
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP