Referencing "used" form fields in Word

Sunjinsak

Board Regular
Joined
Jul 13, 2011
Messages
143
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
Platform
  1. Windows
Hello all…
<o:p></o:p>
Firstly, this isn’t strictly speaking an Excel question. It does involve Excel but if you read on you’ll see that the code/solution I’m after has to be in a Word document – the Excel side of things I can handle. Thought I’d post it here anyway though as this is the most helpful community I’ve come across with regard to Excel/Office and VBA! That said; mods please accept my apologies if you don’t think this is an appropriate place for this question and feel free to move/delete. Thanks.
<o:p></o:p>
<o:p>Using Office 2003 on Windows XP
</o:p>
We have a spreadsheet for individuals to record certain information on. This info has to be collated and reported at the end of each month on a report form which is a Word document.
<o:p></o:p>
I’ve written code to take the entries from the spreadsheet and put them in the Word doc using form fields with bookmark names.
<o:p></o:p>
The code looks a little like this (just for example)…
<o:p></o:p>
Code:
[FONT=Arial][SIZE=3][COLOR=#000000]Set ReportDoc = wdApp.Documents.Add("C:\Path\To\Document\ReportDoc.doc")[/COLOR][/SIZE][/FONT]
[SIZE=3][COLOR=#000000][FONT=Arial]   With ReportDoc.Bookmarks[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Arial]   .Item("Name").Range = NameVariableGoesHere[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Arial]   .Item("MonthEnd").Range = MonthEndVariableGoesHere[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Arial]   .[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Arial]   .[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Arial]   .[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Arial]   etc[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Arial]   End With[/FONT][/COLOR][/SIZE]
<o:p></o:p>
So as you can see I have named form fields in the Word doc where specific entries are made. The problem is, once those entries have been made the form field disappears so can no longer be referenced.
<o:p></o:p>
The report forms are e-mailed off and then used to complete a master spreadsheet by one individual in a different office. I’ve been asked by that individual to automate that process as well. So I’m basically looking to reverse the process of importing the info into the Word doc. The idea being that there will be a new button on the Word doc that will call a sub to do the dirty work.
<o:p></o:p>
I have no problem at all referencing the master spreadsheet and writing code to put the info where it needs to go etc… the problem I have got though is actually referencing the info in the Word doc in the first place as the form fields disappear as soon as they’re populated with the initial import of info from the first spreadsheet.
<o:p></o:p>
Anyone know how I can get around this problem?
<o:p></o:p>
I hope I’ve explained it enough but feel free to fire a few question marks at me if you need more info/clarification.
<o:p></o:p>
Just one thing to add – I can’t “cut out the middleman” by referencing or linking to the master spreadsheet direct from our spreadsheets as we have no shared network storage between offices, that’s why the info has to be transposed to a Word doc and sent via e-mail to be entered on the master sheet at another site.
<o:p></o:p>
Thanks in advance.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Anyone?

If there's no way to keep the form fields after the intial import of data from Excel to the Word doc, is there anything I can replace the form fields with that'll perform the same function but remain after the import so I can reference them again when exporting the data?
 
Upvote 0
Code:
[FONT=Arial][SIZE=3][COLOR=#000000]Set ReportDoc = wdApp.Documents.Add("C:\Path\To\Document\ReportDoc.doc")[/COLOR][/SIZE][/FONT]
[SIZE=3][COLOR=#000000][FONT=Arial]   With ReportDoc.Bookmarks[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Arial]   .Item("Name").Range = NameVariableGoesHere[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Arial]   .Item("MonthEnd").Range = MonthEndVariableGoesHere[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Arial]   .[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Arial]   .[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Arial]   .[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Arial]   etc[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Arial]   End With[/FONT][/COLOR][/SIZE]
<o:p></o:p>
So as you can see I have named form fields in the Word doc where specific entries are made. The problem is, once those entries have been made the form field disappears so can no longer be referenced.
I tried to reproduce the problem, but couldn't - the bookmarks (form fields) are retained in the active document and in the saved document. So this works for me (I put the code in an Excel module, but I'm not sure where your code is meant to be).

Rich (BB code):
Set ReportDoc = wdApp.Documents.Add("C:\Path\To\Document\ReportDoc.doc")
    With reportDoc.Bookmarks
        .Item("Name").Range = "ABCD"
       .Item("MonthEnd").Range = "11"

'Reference the same bookmarks
        .Item("Name").Range = "1234"
       .Item("MonthEnd").Range = "12"
    End With
I saved the Word document and ran the above code successfully again, showing that the bookmarks still exist in the document.
 
Upvote 0
Weird – doesn’t work for me.
<o:p> </o:p>
Although there are a couple of differences that may account for that…
<o:p> </o:p>
Firstly, the code to grab the data from the spreadsheet and then place it in the Word doc is in a module in Excel, whereas the code to export the data in the Word doc to another Excel sheet is (or will be, if I can get it to work) in a module in the Word doc. It has to be this way as the Word doc will be shipped off via e-mail to another office where the export will take place.
<o:p> </o:p>
Secondly, and I’m really not sure if this’ll make a difference or not, is that in your code example you have the values hard coded, whereas I’m reading the values in off the spreadsheet and performing some string manipulation on them (or at least most of them). Therefore I have to store the values as variables.
<o:p> </o:p>
It’d be so much easier to explain if I could just post either all of the code or even the spreadsheet/Word docs themselves but unfortunately I can’t for confidentiality reasons - even redacting them is out of the question.
<o:p> </o:p>
Thanks for getting back to me anyway – and sorry for the late reply but I’ve not been in work for a few days.
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,376
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top