+ Reply to Thread
Results 1 to 12 of 12

Copy Excel data to Word fields and retain fields

  1. #1
    Registered User
    Join Date
    11-25-2011
    Location
    Lilydale, Victoria, Australia
    MS-Off Ver
    2013 (365)
    Posts
    63

    Copy Excel data to Word fields and retain fields

    I want to set up an Excel 2003 worksheet as a database and copy values (name, date of birth, employer, etc) from there to three Word 2003 templates (questionnaire, letter and report), and to Access. I think the best way to copy to Word is to set up fields and cross references (I want to use the same item of data more than once in each doc) and send the data to the fields. I have succeeded in copying the data to the fields (see sample code below), but it replaces the field, and then the cross references don't work. Does anyone know of a way to copy across to Word without removing the fields?
    Thanks.

    Please Login or Register  to view this content.

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Copy Excel data to Word fields and retain fields

    Use docvariables

    Please Login or Register  to view this content.
    Insert docvariables in the location & formatting you want in the Worddocument: menubar/insert/fields/docvariable/ name "name"
    You can use docvariables as often as you like (no referencing needed).



  3. #3
    Registered User
    Join Date
    11-25-2011
    Location
    Lilydale, Victoria, Australia
    MS-Off Ver
    2013 (365)
    Posts
    63

    Smile Re: Copy Excel data to Word fields and retain fields

    Thanks snb. Finally got it to work after a few hours of trying. Actually, it worked the first time, but no more after that.
    Eventually discovered that I also needed:
    Please Login or Register  to view this content.
    to make the entries update.

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Copy Excel data to Word fields and retain fields

    That is correct.
    And if you want them to be unchanged after that you can use:

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    11-25-2011
    Location
    Lilydale, Victoria, Australia
    MS-Off Ver
    2013 (365)
    Posts
    63

    Re: Copy Excel data to Word fields and retain fields

    Excellent. Thanks again.

  6. #6
    Registered User
    Join Date
    11-25-2011
    Location
    Lilydale, Victoria, Australia
    MS-Off Ver
    2013 (365)
    Posts
    63

    Re: Copy Excel data to Word fields and retain fields

    Is .fields.unlink supposed to remove DocVariables from a Word document. I put it into my VBA code, but the fields remain. Do I have to select the fields in the document first? If so,how is that done? I've been trying, but I can't get it.
    I found this bit of code to try selecting Word text, but I keep getting a compile error: "Variable Not Defined" on "ActiveDocument" after "Start:=". I don't know how to define it. Using Office 2003.

    Please Login or Register  to view this content.

  7. #7
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Copy Excel data to Word fields and retain fields

    if you use

    Thisdocument.fields.unlink

    The fields will be converted to their values. No fields left.

    Don't use Select or activate in VBA.

    but:

    Please Login or Register  to view this content.
    Last edited by snb; 06-05-2012 at 10:04 AM.

  8. #8
    Registered User
    Join Date
    11-25-2011
    Location
    Lilydale, Victoria, Australia
    MS-Off Ver
    2013 (365)
    Posts
    63

    Re: Copy Excel data to Word fields and retain fields

    Thanks, snb. I tried .fields.unlink again, and this time it decided to work. Hmmm...

    This has raised another issue or two, however. I see that doing that has also removed the fields from my Table of Contents (and probably page numbers) in my Word doc. I will need these later. So I tried to use your code (thanks) to see if I could just remove some of the fields, but I got the same error: "Variable not Defined" on "ActiveDocument". I am using Option Explicit in the code, and it's Office 2003.

    Also, I have used ".activate" about six times in my code, and "select" once (just to put focus into a particular cell before the code finished running). What should I use instead? (One of those .activates is just before .fields.update .fields.unlink)

  9. #9
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Copy Excel data to Word fields and retain fields

    Word contains some 'storyranges'; have a look in the helpfiles about them.
    You can restrict the unlinking to 1 or more of these storyranges.
    But why would you like to 'unlink' ?

  10. #10
    Registered User
    Join Date
    11-25-2011
    Location
    Lilydale, Victoria, Australia
    MS-Off Ver
    2013 (365)
    Posts
    63

    Re: Copy Excel data to Word fields and retain fields

    Quote Originally Posted by snb View Post
    But why would you like to 'unlink' ?
    One of the documents filled from VBA is the template of what will eventually be a large Word document. When filled from VBA, there is still a lot more work to do on the document. VBA only puts some helpful bits of information in there (names, addresses, correct pronouns, etc). After the document is completed, it gets formatted by admin staff. They have had trouble with other fields in documents - although these were a different type. The trouble has come when they want to modify something in one of the fields, and it breaks links to other fields in the document, and that causes errors. So I thought it would be best to remove DocVariables when it gets to that stage.

    You've got me thinking now, though. I wonder if DocVariables would give the same trouble. I'd better do some experimenting... or just wait till the thing is up and running, and see what happens.

    These documents are sometimes sent to clients as Word docs as well (rather than PDF). I wonder if there will be any issues there.

    Now, about not using .activate in VBA...?

  11. #11
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Copy Excel data to Word fields and retain fields

    If it is your preference after successfully linking and merging to have the fields unlink, have your admins 'Save As' as a different word document (to preserve the template nature of the original document), then Ctrl+A to select all, then Ctrl+6 to remove all links. 10 seconds of total labor time.

    If I have a field linked to, say, the named range EmployeeName, and that field is styled to, say, Header 1, Ctrl+6'ing the link to de-link will not affect (or at least hasn't in my experience) the table of contents tied to a hypothetical Table of Contents which uses Header 1 as level 1.

    Now, I cannot speak to an instance whereby the document contains hyperlinks or bookmarks to that location, which was formerly linked and subsequently de-linked and what, if any, issues arise.

    You might also be interested in Format Field Switches here: http://www.gmayor.com/formatting_word_fields.htm
    Last edited by AlvaroSiza; 06-06-2012 at 01:10 AM.
    Perhaps it was the Noid who should have avoided me...
    If you are satisfied with my solution click the small star icon on the left. Thanks
    1. Make a copy of your workbook and run the following code on your copy (just in case)
    2. With excel open, press ALT+F11 to open the Visual Basic Editor (VBE). From the "Insert" menu, select "Module".
    3. Paste the code from above into the empty white space. Close the VBE.
    4. From the developer tab, choose "Macros", select the Sub Name, and click "Run".

  12. #12
    Registered User
    Join Date
    11-25-2011
    Location
    Lilydale, Victoria, Australia
    MS-Off Ver
    2013 (365)
    Posts
    63

    Re: Copy Excel data to Word fields and retain fields

    Thanks AlvaroSiza. I had been hoping to do it through VBA so that no one would have to remember to do it, etc. I think snb might have talked me out of doing it anyway, as it might be OK to leave them there.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1