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.

The code uses the ActiveDocument object, from the reading I've done I
either need to code to set the focus on the correct documents in
between the copy and paste lines, or I should refer to the documents
directly rather than use ActiveDocument.

But I'm hazy about how to do either of them!

And just to make it more complex, these calls are being made from
within an Access application.

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.

'I should set the focus to the reference document here, how?

CopyText = ActiveDocument.Bookmarks(BookmarkToCopy).Range.Text

'I should set the focus to the destination document here, how?

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
 
T

Travis

Terribly sorry about the repetition everyone. Google Groups has been
playing up lately, with posts taking 24 hours to appear and some
seeming not to appear at all. In this case there was an error message
saying my post had failed so I tried again until I didn't get the error
message. Evidently the error message was wrong!

Travis
 

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