Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing data from one open word document to another

Status
Not open for further replies.

JohnnyRazzle

Programmer
May 4, 2005
10
GB
Hi folks, this is my problem. I have a word document that contains form fields. This document will have a macro that will open up another document. The new document using its own macro, will use the data entered into the first document by the user. How do you access data from different documents?

I could do it with temp text files, but i reckon there would be a cleaner way of doing it than that built in???
 
Take a look at the FormFields collection of the Document object.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Im afraid not amigo...
...i can access the formfields in the original document using code within that documents editor:

ActiveDocument.FormFields.Item(1).Result

however, i dont know how to access the formfields through another document....i assume there must be something to replace the ActiveDocument in the above line?
Thanks
 
Take a look at the Documents collection of the Word.Application object.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi JohnnyRazzle,

You could also use a series of INCLUDETEXT fields in the second document to automatically insert the data from the source document. To do this, the source document would have to have been saved and its name and path would have to be known to the target document.

Cheers
 
PH has the right idea.

You do not state whether the second document is opened, or created. You say opened, then refer to it as new. Let's assume it IS an existing document and you are opening it.

You do not state why the macro to get the data is in the second document.

From the sound of it, the macro in the first opens the second...then stops. Are you manually switching to the second (keeping the first open) and, sometime down the road, running code to get data from the first? Why not run the macro from the first? Have it open the second document, and pass the data to it. Something like:
Code:
Dim ThisDoc As Document
Dim ThatDoc As Document
Set ThisDoc = ActiveDocument

Documents.Open FileName:="f:\test2.doc"
Set ThatDoc = ActiveDocument

ThatDoc.FormFields("Text3").Result = _
    ThisDoc.FormFields("Text1").Result
    ' etc etc
Set ThatDoc = Nothing
Set ThisDoc = Nothing

However, if there is a legitimate reason for it, you can still get at the data from the first. You could:

a) have the macro in the second call back to a procedure in the first. That procedure could build an array of formfield results, and return it.

OR
b) using the Document collection, do the same thing from the first code box above, but from the second document.
Code:
Dim SecondDoc As Document
Dim FirstDoc As Document

Set SecondDoc = ActiveDocument
Set FirstDoc = Documents.Item("Test1.doc")

SecondDoc.FormFields("Text3").Result = _
    FirstDoc.FormFields("Text1").Result

Set FirstDoc = Nothing
Set SecondDoc = Nothing

Gerry
See my Paintings and Sculpture
 
Cheers for the tips guys. I ended up using this line:

Documents.Item(2).FormFields.Item(1).Result

It seemed to work ok, the Item(2) was referring to the first document. It seemed to work ok, even if there were other word documents open before, so i guess they werent part of this collection, although i dont understand why? (like i said im a bit of a newbie at vb and collections :)
 
Yes:

Documents.Item(2).FormFields.Item(1).Result

will work. BUT, and it is a big but. It works on the SECOND document (of the Document collection), and with the FIRST formfield of that document.

There is nothing [specific, or rather explicit) about this. Fine, absolutely fine, if what you want is the first formfield of the second document.

What PH and I were trying to get at was the use of EXPLICIT (THAT document...rather that the second one of the collection) instructions.

Item2 refers to the first document, because the second document is now the first in the collection. It is in order of creation. As this can get confusing, or complicated, it is MUCH better to use explicit pointers.

Documents.Item(2).FormFields.Item(1).Result
vs
Documents("ThisDocument.doc").FormFields("ThisField").Result

Are you aware of the fact that Word RENAMES formfields if you move the formfield within the document, OR if you add a new formfield before an existing one. Explicit names prevent confusion and help with locating problems.

Finally, there is something odd in your last sentence. All documents that are open with the same instance of Word are in the same Documents collection.


Gerry
See my Paintings and Sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top