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
 
D

Doug Robbins - Word MVP

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


--
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
 
T

Travis

Doug said:
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
 
D

Doug Robbins - Word MVP

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.


--
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
 
H

Helmut Weber

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

--

Helmut Weber, MVP WordVBA

"red.sys" & chr$(64) & "t-online.de"
Win XP, Office 2003
 

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