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
Doug Robbins - Word MVP - 09 Jan 2006 12:57 GMT
Assuming that the bookmark names are the same in both of the documents, the
following should do what you want.
Dim Source as Document, Target as Document
Dim bm as Bookmark
Dim bmName as string
Set Source = Documents.Open("path\filename of the source document")
Set Target = Documents.Open("path\filename of the target document")
For Each bm In Source.Bookmarks
bmName = bm.Name
Target.Bookmarks(bmName).Range = bm.Range
Next bm

Signature
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
> I've written a sub which copies text from one enclosing bookmark to
> another. (Code at end of this question).
[quoted text clipped - 55 lines]
>
> End Sub
Travis - 09 Jan 2006 13:06 GMT
Travis - 10 Jan 2006 04:20 GMT
> Set Source = Documents.Open("path\filename of the source document")
> Set Target = Documents.Open("path\filename of the target document")
Doug,
Does this code assume that both the documents I am using are open? (As
in, open and visible to the user)
I would want Source to remain invisible, e.g. no open Source window.
Do I still do it in that way or can documents be opened without
appearing to be open?
Travis
Doug Robbins - Word MVP - 10 Jan 2006 05:22 GMT
The code will open the documents if you have the correct path
filenames specified. If you do not want the user to see the source
document, use
Dim Source as Document, Target as Document
Dim bm as Bookmark
Dim bmName as string
Set Source = Documents.Open("path\filename of the source document")
Set Target = Documents.Open("path\filename of the target document")
Target.Activate
For Each bm In Source.Bookmarks
bmName = bm.Name
Target.Bookmarks(bmName).Range = bm.Range
Next bm
Source.Close wdDoNotSaveChanges
and the user will probably not see the source document, or only very briefly
while the target document is being opened.

Signature
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
>> Set Source = Documents.Open("path\filename of the source document")
>> Set Target = Documents.Open("path\filename of the target document")
[quoted text clipped - 9 lines]
>
> Travis
Helmut Weber - 10 Jan 2006 12:39 GMT
Hi Travis,
you can open documents whithout showing them to the user at all.
Dim oDcm As Document
Set oDcm = Documents.Open("c:\000\fish-010.doc", Visible:=False)
oDcm.Close
Or you can hide the entire application:
Application.Visible = False
Dim oDcm As Document
Set oDcm = Documents.Open("c:\000\fish-010.doc")
oDcm.Close
Application.Visible = True
HTH

Signature
Helmut Weber, MVP WordVBA
"red.sys" & chr$(64) & "t-online.de"
Win XP, Office 2003