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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copying bookmarked data from 1 Word doc to another 2

Status
Not open for further replies.

PCVirgin

Programmer
Nov 11, 2003
75
0
0
GB
I am new to VBA and I hope I have posted this in the right forum.

I am trying to copy text from one opened Word document to another using a macro. In the macro I check to see if the source document is opened then move the data at each specified bookmark to another specified bookmark in the destination document.

When I first ran this code it 'worked' like a charm. Now I keep getting the spurious message saying "5941"

Can anyone help me?
__________________________________________________________
Private Sub Document_Open()
Dim strDocName As String
strDocName = "U:\Work\LHN Assessment - MASTER.doc"
If DocOpen(strDocName) = True Then
Dim appWord As Word.Application
Dim SrcDoc As Word.Document
Dim DestDoc As Word.Document
Set DestDoc = ActiveDocument
Set SrcDoc = Documents.Open("U:\Work\LHN Assessment - MASTER.doc")
' Set appWord = CreateObject("Word.Application")
'Sex'
DestDoc.Bookmarks("Text473").Range.Text = SrcDoc.Bookmarks("Text318").Range.Text

DestDoc.Bookmarks("Text487").Range.Text = SrcDoc.Bookmarks("Chk103").Range.Text

Else
MsgBox "LHN Assessment Document is closed"
End If

End Sub
 
Hi PCVirgin,
How do I attach the documents so you can see the values of the fields?
You can't.

In any event, that's not what I asked for. All I need to know in the filename & path of the document containing the formfields into which you're doing the data entry, plus the bookmark names for a selection of those formfields.

PS: With a checkbox formfield, though, it's best to bookmark the whole formfield rather than relying on the bookmark name that Word ordinarily assigns to the checkbox.


Cheers
[MS MVP - Word]
 
Path of destination document
Code:
C:\Users\Larry\Documents\NHS\DST word.doc

Formfield Bookmark names =
Code:
 Text475 thru Text490 & Check300 thru Check341

Bookmarks =
Code:
NHSNo, DoB, FamilyName, Forename
 
Hi PCVirgin,

I asked for:
the filename & path of the document containing the formfields into which you're doing the data entry, plus the bookmark names for a selection of those formfields
I did not ask for anything to do with the destination document. I don't need that.


Cheers
[MS MVP - Word]
 
Code:
C:\Users\Larry\Documents\NHS\LHNA tools.doc

Formfield Bookmark names =
Code:
 Text475 thru Text490 & Check300 thru Check341

Bookmarks =
Code:
NHSNo, DoB, FamilyName, Forename
 
Hi PCVirgin,

OK, in your destination document(DST word.doc), at the location where you want to replicate the 'Text475' bookmark contents:
1. Press Ctrl-F9 twice to create a pair of nested field braces, which will look like '{ { } }'.
2. Fill in/around the field braces thus-
Code:
{INCLUDETEXT "{FILENAME \p}\\..\\LHNA tools.doc" "Text475" \!}
3. Select the filed and press F9 to update. It should immediately reflect whatever the contents are of the corresponding field in your 'LHNA tools' document.

Do likewise for the other bookmarks also (eg
Code:
{INCLUDETEXT "{FILENAME \p}\\..\\LHNA tools.doc" "NHSNo" \!}

None of this uses any formfields in your destination document(DST word.doc).

As advised in a previous post, you'll need to bookmark your checkbox formfields in your 'LHNA tools' document and not rely on the formfield bookmark names.

When you're done, go to Tools|Options|General and make sure the 'update automatic links at open' option is checked.

Now you can save and close the destination document(DST word.doc). From now on, whenever you open the destination document, it will reflect the current contents of the various bookmarks in your 'LHNA tools' document.

That's all there is to it. No macros are needed.

Note: Ordinarily, an INCLUDETEXT field would be coded along the lines of:
Code:
{INCLUDETEXT "C:\\Users\\Larry\\Documents\\NHS\\LHNA tools.doc" "NHSNo" \!}
That's what Word's Help file would suggest. You could also use:
Code:
{INCLUDETEXT "C:/Users/Larry/Documents/NHS/LHNA tools.doc" "NHSNo" \!}
But the insertion of the FILENAME field in the way I've shown means that, provided the two documents remain in the same folder, the links will continue to work even if the filepath changes.



Cheers
[MS MVP - Word]
 
Thanks to Fumei & Macropod for your patience.

I now have a document that after User input can have certain parts of that input 'copied' directly into another document once it's opened.

One final thing, I have hardcoded the filename in the destination document. Can I use similar coding to the INCLUDETEXT code ----
Code:
 FILENAME \p}\\..\\LHNA tools.doc" ???
 
Hi PCVirgin,
Can I use similar coding to the INCLUDETEXT code[\quote]I take it you're after a vba equivalent to the source document's path, which is the same as the target document's path? In that case, in either document, you can retrieve the path with:
ActiveDocument.Path
So:
strDocName = ActiveDocument.Path & "\LHNA tools.doc"


Cheers
[MS MVP - Word]
 
And that folks, is it.

I now have my two documents working as I planned.

Thanks for your assistance........................

Until my next project.......
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top