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
 
I would like to help, and probable could, but since you totally ignored everything I wrote in your other post, I fail to see much point.

BTW: the 5941 error from your code is not spurious whatsoever. It is quite appropriate and correct.

The requested member of the collection does not exist.

Gerry
 
There seems to be some confusion here, as I am not talking about check boxes here, simply moving data from one word document to another.

Your comments from a previous post were taken on board however this IMHO is another subject.

The error suggests that I have not coded the move correctly as the data contains more than one word. I have no clue how to code the move correctly.

If you can help then I would appreciate it.

 
1. "The error suggests that I have not coded the move correctly as the data contains more than one word. "

How on earth did you come up with that? It does not suggest that at all. It suggests exactly as it states: The requested member of the collection does not exist.

It has nothing to do with the amount of words.

2. you did not respond at all to my posted answer to your other post, so I do not know if these are indeed real bookmarks, or they are (as I suspect they are) formfields. And, as I wrote, if they ARE formfields, then use them as formfields.

3. are you, or are you not creating an instance of Word?
Code:
  Dim appWord As Word.Application
If you are not (and I see no reason for you to do so), then remove this.

4. what is: SrcDoc.Bookmarks("Chk103")?

Is it a real bookmark? The name "Chk103" implies it is a checkbox, but is it? As I stated in the other post a checkbox is NOT a bookmark, although a checkbox formfield HAS a bookmark.

The Range.Text property of that bookmark returns something like: FORMCHECKBOX


Try actually answering questions asked of you, and you may get some help.

1. are these formfields, or not?
2. how do you know the specified bookmarks exist? You can check for this.
Code:
.Bookmarks.Exists([i]name[/i])
BTW: please use the TGML tags when posting code. Thanks. If you do not know how to do this, click the Process TGML link on your posting page. It is just below the window you type in.

Gerry
 
I appreciate your cander as this can only speed up matters, however my comment of "I am new to VBA " seems to have been ignored. I am neither deliberately coding this incorrectly nor wasting your time. I am trying to template a way of copying typed information from one document to another.

Going through your questions 1 by 1.
1) The fields in the source document are FORMTEXT & FORMCHECKBOX fields.

2) Why is this statement in the code "Dim appWord As Word.Application" I've no clue. I was advised to code it by someone ages ago. I will remove it.

3)My orginal bookmarks were called TEXT for text fields and CHK for check boxes. I've subsequently removed the CHK references and called them CHECKnnn in a sequential order.

Bookmarks do exist in the document because I have added them. Whether I need them after your help is another matter.


Since the initial post I have reformatted the document so that the bookmarks in the checkboxes is no longer a problem. I am doing a checkbox value to checkbox value move instead.

My current problem is moving the values from the FORMTEXT fields to the second document.

Would it help if you saw the documents in question?
 
3)My orginal bookmarks were called TEXT for text fields and CHK for check boxes.
1. I will repeat what I posted in the other thread, and here as well.

These are formfields. Use them as such. I gave an explicit example (and repeat it below).

2. If you have that error, then the bookmark does not exist.

BTW: "called them CHECKnnn in a sequential order."

This is a good idea.

Perhaps you can explain what you want to do. What is your intention?

The field [INCLUDETEXT] can, well, include text, from one document to another. INCLUDETEXT targets a bookmark - a real bookmark. But then...you are not using bookmarks really, you are using - it seems - formfields.

I understand that you stated "I am new to VBA". That is OK, we were all that way at the start. It would help (you) if you listened though. Here is a repeat of what I responded with
Code:
  DestDoc.[b]FormFields[/b]("Text473").[b]Result[/b] = _
        SrcDoc.[b]FormFields[/b]("Text318").Result
But if one of those does not exist, then it does not exist, and you get that error. I suspect (because of those high numbers) that there was some copy and paste done. Copy and pasting formfields is a bad idea because then you have TWO of them. They must have a unique name (bookmark), thus ONE of them is rendered with no bookmark.

I have to ask though, why are formfields being used? They are for user-input. Are the users actually going to input things into these? Changing what you may have copied over?
My current problem is moving the values from the FORMTEXT fields to the second document.
This is a commonly done thing, and is pretty straightforward.

The text value of a text formfield is the .Result property. So, to repeat
Code:
  DestDoc.[b]FormFields[/b]("Text473").[b]Result[/b] = _
        SrcDoc.[b]FormFields[/b]("Text318").Result
does EXACTLY that: "moving the values from the FORMTEXT fields to the second document"

Gerry
 
In answer to part of your question:

"I have to ask though, why are formfields being used? They are for user-input. Are the users actually going to input things into these? Changing what you may have copied over?"

The senario is:

There is an initial form called 'LHNA' and data/information from this is passed to another form called DST. It is then optional for the User to keep all the information copied over. Also as I cannot code the second document to protect certain fields I'll leave it as that.

I will revisit the names & locations of each bookmark in both documents before I reply to your tip.


This has been a long drawn out process for me and with your assisatnce I believe I will be finished soon.


Thank you for your time.
 
The revised code (hopefully coded correctly now)


Private Sub Document_Open()
Dim strDocName As String
strDocName = "C:\Users\Larry\Documents\NHS\LHNA tools.doc"

Dim SrcDoc As Word.Document
Dim DestDoc As Word.Document
Set DestDoc = ActiveDocument
Set SrcDoc = Documents.Open("C:\Users\Larry\Documents\NHS\LHNA tools.doc")

'Sex'
DestDoc.FormFields("Text475").Result = _
SrcDoc.FormFields("Text475").Result


End Sub


I ran this but 'nothing' happened! Well I say nothing, I did not get an error message for the first time.

What am I doing wrong?
 
1. Please use the TGML code tags. Thanks.

2. the code - amended to actually use the string variable you declare and give a value....
Code:
Option Explicit

Private Sub Document_Open()
Dim strDocName As String
strDocName = "C:\Users\Larry\Documents\NHS\LHNA tools.doc"
  
  Dim SrcDoc As Word.Document
  Dim DestDoc As Word.Document
  Set DestDoc = ActiveDocument
  Set SrcDoc = Documents.Open(strDocName)

   DestDoc.FormFields("Text475").Result = _
        SrcDoc.FormFields("Text475").Result
End Sub
works perfectly.

1. I created the folder C:\Users\Larry\Documents\NHS.

2. I created a document with a formfield named Text475, put a text string into that formfield ("Gerry"), and saved it as LHNA tools.doc

3. I created a new document with a formfield named Text475, put in the Document_Open procedure above, and saved the file as tekTips.doc.

4. I close that file and then opened it again.

I repeat. The procedure works perfectly.

It opened LHNA tools.doc and put "Gerry" into Text475 of the ActiveDocument (tekTips.doc).

What am I doing wrong?
You are doing nothing wrong, except for declaring your string variable strDocname and never using it.

If "nothing" is happening it is for one reason. There is nothing there TO happen. Text475 in LHNA tools.doc must be empty.

The procedure works EXACTLY the way it is supposed to work.

Good luck.

Gerry
 


"PC Virins" today, have it so much easier than us "mainframe virgins" did some 4 decades ago.

First you wrote your program on a Code Sheet.

Then you punched each line of code into a Hollerith card, commonly referred to as an IBM card.

Then you submitted your deck of cards to the input desk.

2 to 24 hours later you got a printout of your results. If you were skilled at coding, (your statements were syntactly correct as well as correct logic), you got good results. Otherwise, you got cryptic error messages regarding your syntax or other gobblygook.

You and I today, can step thru our code and see exactly what is happening. Here's another tip. faq707-4594

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Indeed. If you put in (for testing purposes, as if you use Document_Open the code will execute without a chance for you to use the watch window):
Code:
Private Sub TestWatch()
Dim strDocName As String
strDocName = "C:\Users\Larry\Documents\NHS\LHNA tools.doc"
  
  Dim SrcDoc As Word.Document
  Dim DestDoc As Word.Document
  Set DestDoc = ActiveDocument
  Set SrcDoc = Documents.Open(strDocName)

   DestDoc.FormFields("Text475").Result = _
        SrcDoc.FormFields("Text475").Result
End Sub
in your ActiveDocument (your DestDoc object) and put a Watch on DesDoc.FormFields("Text475"), you can see the changes in the Watch window.

Say it starts out as "" - empty. Expanding the Watch window item (so you can look at Result), you can see the Result value = "" as you Step through. When the code hits
Code:
   DestDoc.FormFields("Text475").Result = _
        SrcDoc.FormFields("Text475").Result
poof! Voila! The watch windows shows the result change to "Gerry" - the value taken from SrcDoc.Formfields("Text475").

Which also, BTW, can now be seen in the formfield in the document.

Gerry
 



Gerry, I'm with the Voila, although here in Texas, we say "Vola, y'all!"

But *poof* ???

Wuzzat?

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
It is that puff of smoke (going poof) when you do magic. It has nothing to do with poofta in case our friends across the pond get funny ideas.

I.e. the Watch window is magic.

Gerry
 
BINGO......."You are doing nothing wrong, except for declaring your string variable strDocname and never using it."

I removed the code for strDocname and it worked a treat.

I'm glad my problem has been the source of such merriment to you all.

PS Once again my statement of being new to VBA is overlooked. You said "put a Watch on DesDoc.FormFields("Text475")," I've no idea how to code that, so your wise words were not used.

Now can I combine the code I've just 'perfected' with the coding for the checkboxes?

Sub test()

Dim DocA
DocA = Dir(""C:\Users\Larry\Documents\NHS\LHNA tools.doc")

Dim DocB
Set DocB = ActiveDocument


Dim objA As FormField
Dim objB As FormField

For Each objA In DocA.FormFields

For Each objB In DocB.FormFields

If objA.Name = objB.Name Then
objB.CheckBox.Value = objA.CheckBox.Value
Exit For
End If
Next
Next

End Sub
 
Hi PCVirgin,

Are you aware that you can replicate the source document's bookmarked ranges in the target document via INCLUDETEXT fields, without the need for vba?


Cheers
[MS MVP - Word]
 
Thanks but do you think I would have gone through all this had I known?

It would be useful if people gave an actual example of the code they are talking about when advising on what to do. I spend ages trying to find the correct syntax after a tip.

What about combining what I have coded previously with this?

Code:
Sub test()

Dim DocA
DocA = Dir(""C:\Users\Larry\Documents\NHS\LHNA tools.doc")

Dim DocB
Set DocB = ActiveDocument


    Dim objA As FormField
    Dim objB As FormField
    
    For Each objA In DocA.FormFields
        
        For Each objB In DocB.FormFields
            
            If objA.Name = objB.Name Then
                objB.CheckBox.Value = objA.CheckBox.Value
                Exit For
            End If
        Next
    Next

End Sub
 
Hi PCVirgin,
Thanks but do you think I would have gone through all this had I known?
We're not mindreaders. You posted in a vba forum seeking a vba solution, in effect ruling out a non-vba solution.

The lesson you should take away from this is to set out the details of the outcome you're trying to achieve instead of limiting yourself to a particular method of achieving it - especially when you don't have a good grasp of the solution you're proposing.
It would be useful if people gave an actual example of the code they are talking about when advising on what to do. I spend ages trying to find the correct syntax after a tip.
So far, you haven't posted enough detail for anyone to reply with an INCLUDETEXT solution. If you care to post the full path and filename of the document containing the formfields, plus the bookmark names associated with one or more of them, I'll happily show you how the INCLUDETEXT field in your 'LHNA tools' document might be coded.

I'll leave it to others to provide detailed comment on your code, but it seems to me you haven't tested it; otherwise you'd know it doesn't work. Hint: your latest code doesn't even open the 'LHNA tools' document, so there's no chance of it updating any formfields there.



Cheers
[MS MVP - Word]
 
Ok folks, this is getting out of hand now.

Initially I had a simple request. Obviously I did not put that request in the correct syntax for the forum but I am learning.

We're not mindreaders. You posted in a vba forum seeking a vba solution, in effect ruling out a non-vba solution.

I was advised to use a VBA solution - hence my initial post. At no stage did anyone suggest to me that there was/is another way.

After slight confusion "fumei" has helped me achieve most of my goal. If he or anyone else can assist me in finishing my documents I'd appreciate it.

but it seems to me you haven't tested it; otherwise you'd know it doesn't work. Hint: your latest code doesn't even open the 'LHNA tools' document, so there's no chance of it updating any formfields there.

That is not my aim. I am only trying to update the Dest document.

If you care to post the full path and filename of the document containing the formfields, plus the bookmark names associated with one or more of them, I'll happily show you how the INCLUDETEXT field in your 'LHNA tools' document might be coded.

In which forum shall I post it?
 
Hi PCVirgin,

In which forum shall I post it?
Sincde this thread is already active, you may as well do it here.


Cheers
[MS MVP - Word]
 
It's just not my week.

How do I attach the documents so you can see the values of the fields?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top