Working with two or more Word docs.

T

Travis

I've written a sub which copies text from one enclosing bookmark to
another. (Code at end of this question).

While I haven't run it yet, it was based quite closely on some code
snippets I found at various VBA sites so I think it should work (but if
anyone sees problems with it, do tell!). That's not my question
though.

My question is about how to refer to bookmarks in two different
documents. If I'm copying within the current document then everything
should be ok, but the text I'm copying resides in a different document
to the one where I want to paste it.

In fact, the document I want to paste to is a copy (created at runtime)
of a template document but the document I'm copying text from is a
permanent text library.

What I'm wondering about is how to refer to the two (or more) open
documents and tell Word which one I'm wanting to use.

If I give each of the bookmarks a unique name then is it merely enough
to specify the bookmark name (e.g. I make sure that the bookmark "foo"
only appears in one document and "bar" only in the other one) or do I
call them by full paths etc.

Travis

My bookmark text copying code follows:

Sub CopyBookmarkToBookmark(BookmarkToCopy As String, BookmarkToPaste As
String)
Dim CopyText As String
Dim PasteRange As Range

'In order to retrieve the text in a bookmark, the bookmark needs to be
'an enclosing bookmark.

CopyText = ActiveDocument.Bookmarks(BookmarkToCopy).Range.Text
Set PasteRange = ActiveDocument.Bookmarks(BookmarkToPaste).Range
PasteRange.Text = CopyText

'This next line puts the bookmark back after it was overwritten by the
above.
ActiveDocument.Bookmarks.Add BookmarkToUpdate, PasteRange

Set CopyText = Nothing
Set PasteRange = Nothing

End Sub
 

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