473,386 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Importing data from Word form content control into Access

15
I am trying to take the user input from a Word form and place it into an Access DB. I have the document and database in the same folder on my C drive. I am getting an error at :

rst.Fields("FullName") = doc.FormFields("txtYourName").Result

It is not getting the data from the content control.

Here is all of the of the code:

Expand|Select|Wrap|Line Numbers
  1. Sub GetWordData()
  2.  
  3.  
  4. Dim appWord As Word.Application
  5. Dim doc As Word.Document
  6. Dim cnn As New ADODB.Connection
  7. Dim rst As New ADODB.Recordset
  8. Dim strDocName As String
  9. Dim blnQuitWord As Boolean
  10.  
  11.  
  12. On Error GoTo ErrorHandling
  13.  
  14.  
  15.  
  16. strDocName = "C:\SampleForm\" & _
  17.     InputBox("Enter the name of the Word document you want to import:", "Import document")
  18.  
  19. Set appWord = GetObject(, "Word.Application")
  20. appWord.Visible = True
  21.  
  22. Set doc = appWord.Documents.Open(strDocName)
  23.  
  24. Set cnn = CurrentProject.Connection
  25.  
  26. rst.Open "FormData", cnn, adOpenKeyset, adLockOptimistic
  27.  
  28.  
  29.     rst.AddNew
  30.  
  31.  
  32.     rst.Fields("FullName") = doc.FormFields("txtYourName").Result
  33.     rst.Fields("Address") = doc.FormFields("txtYourAddress").Result
  34.     rst.Fields("Phone") = doc.FormFields("txtYourPhone").Result
  35.     rst.Fields("Male") = doc.FormFields("txtMale").Result
  36.     rst.Fields("Female") = doc.FormFields("txtFemale").Result
  37.  
  38.     rst.Update
  39.     rst.Close
  40.  
  41.  
  42.  doc.Close
  43.  
  44. If blnQuitWord Then appWord.Quit
  45. cnn.Close
  46. MsgBox "Request Imported"
  47.  
  48. Cleanup:
  49. Set rst = Nothing
  50. Set doc = Nothing
  51. Set appWord = Nothing
  52.  
  53. Exit Sub
  54. ErrorHandling:
  55.  
  56. Select Case Err
  57. Case -2147022986, 429
  58.     Set appWord = CreateObject("Word.Application")
  59.     blnQuitWord = True
  60.     Resume Next
  61.  
  62. Case 5121, 5174
  63.     MsgBox "You must select a valid Word Document." _
  64.     & "No data imported.", vbOKOnly, _
  65.     " Document Not Found"
  66.  
  67. Case 5491
  68.     MsgBox "The document you selected does not" _
  69.     & " contain the required form fields." _
  70.     & " No data imported.", vbOKOnly, _
  71.     " Fields Not Found"
  72.  
  73. Case Else
  74.     MsgBox Err & ": " & Err.Description
  75.  
  76. End Select
  77. GoTo Cleanup
  78. End Sub
  79.  
Any suggestions would be great. Thanks
Jul 29 '13 #1
17 9819
zmbd
5,501 Expert Mod 4TB
We can not do the basic trouble shooting for you; however,
if you will do the following, it will help us to help you:

As the line containing:
Expand|Select|Wrap|Line Numbers
  1. rst.Fields("FullName") = doc.FormFields("txtYourName").Result
may not be the actual source of the error:

Please place a STOP command at Line 11 and comment out Line 12

Once done
Please execute your code.
Your code will stop at line 11 on the STOP command.
Press [F8] slowly until you reach the line where the error occurs.

Please post the Line # where the error occurs
Please post:
The EXACT ERROR
Title
Number
Description.

Please do not omit or paraphrase any of the error message.
Jul 29 '13 #2
tsnave
15
When I get to the line:
rst.Fields("FullName")=doc.FormFields("txtYourName ").Result
it sends me to the error handling:
Case -21447022986, 429 section. I kept scrolling through and got error 5941: The requested member of the collection does not exist.

Thank you for your response
Jul 30 '13 #3
zmbd
5,501 Expert Mod 4TB
You did not comment out Line12
Place a single quote ( ' ) before the entry in that line.
Then run again

I kept scrolling through and got error 5941: The requested member of the collection does not exist
This leads me to a mis-named control or field.
Which line errored with that?
Jul 30 '13 #4
tsnave
15
I commented out line 12 and the error occurs at:
rst.Fields("FullName") = doc.FormFields("YourName").Result

I took out the txt in front of YourName so it would match the content control exactly.

Should I change FormFields to ContentControl?
Jul 30 '13 #5
zmbd
5,501 Expert Mod 4TB
same error?
Please post the error Title, Number, and text.
Jul 30 '13 #6
zmbd
5,501 Expert Mod 4TB
Which version of Office (or Access and Word) are you using?
If in 2007 or 2010 are you using the "classic" field controls or the newer text control?
Jul 30 '13 #7
tsnave
15
I am getting the same error and I am using Access and Word 2010. Also the "classic" field controls.
Jul 30 '13 #8
zmbd
5,501 Expert Mod 4TB
PLEASE
Post THE TITLE
the ERROR NUMBER
and THE TEXT
Jul 30 '13 #9
zmbd
5,501 Expert Mod 4TB
TsNave:

Make a copy of the word document.
In the copy of the word document please insert a standard code module.
Copy and past the following code block into the document.
Run the code
Follow the instructions.
Expand|Select|Wrap|Line Numbers
  1. Sub GetCCInfo()
  2. Dim cc As ContentControl
  3. Dim fc As Field
  4. Dim ccinfo As String
  5.  
  6. ' Iterate through all the content controls in the document
  7. Debug.Print ">>>Cut and paste - Start<<<"
  8. If ActiveDocument.ContentControls.Count <> 0 Then
  9.     Debug.Print "Start of controls, Count = " & ActiveDocument.ContentControls.Count & vbCrLf
  10.     For Each cc In ActiveDocument.ContentControls
  11.         ccinfo = "<> ID= " & cc.ID & " Title = " & cc.Title & " Tag = " & cc.Tag & " Text = " & cc.Range.Text & vbCrLf
  12.         Debug.Print ccinfo
  13.     Next
  14. Else
  15.     Debug.Print "Start of controls, Count = 0"
  16. End If
  17. If ActiveDocument.FormFields.Count <> 0 Then
  18.     Debug.Print "Start of Fields, Count = " & ActiveDocument.FormFields.Count & vbCrLf
  19.     For Each fc In ActiveDocument.Fields
  20.         ccinfo = "<> Index = " & fc.Index & " result = " & fc.Result.Text & vbCrLf
  21.         Debug.Print ccinfo
  22.     Next
  23. Else
  24.     Debug.Print "Start of fields, Count = 0"
  25. End If
  26.  
  27. Debug.Print ">>>Cut and paste - end<<<"
  28.  
  29. MsgBox "Please open the VBA editor and press <ctrl><g>" & vbCrLf & "Please cut and paste the indicated contents to the thread"
  30. End Sub
  31.  
Jul 30 '13 #10
tsnave
15
That did pull the data from the form. Thank you very much. Now, I just got to put that into Access
Jul 30 '13 #11
tsnave
15
>>>Cut and paste - Start<<<
Start of controls, Count = 5

<> ID= 261426431 Title = YourName Tag = YourName Text = Todd Snavely

<> ID= 846142673 Title = YourAddress Tag = YourAddress Text = 1234 Main St.

<> ID= 2918279744 Title = YourPhone Tag = YourPhone Text = 555-5555

<> ID= 1014266676 Title = Male Tag = Male Text = ?

<> ID= 3052641625 Title = Female Tag = Female Text = ?

Start of controls, Count = 0
>>>Cut and paste - end<<<
Jul 30 '13 #12
zmbd
5,501 Expert Mod 4TB
And that explains why you are getting the error that you are receiving - and exactly what I expected to see.

You are not using the "Classic" or "Legacy" form controls that are hold over from Word/Office 2003. You are using the new Content Controls introduced in 2007/2010.


The Rich and Plain text controls are not part of the fields collection as you are attempting to refer to them in the vba
Note that I'm in design mode here so that you can see the tags on the new style controls.

So, you have a few options.
- We can refer to your controls as is via index.
- We can refer to your controls via the Title (the part -that is seen in normal view by the users when they enter the control for data entry.
- We can refer to the controls via the tag property.
all of which require some re-writing of your code.

OR

- You can redo your word document and use the legacy controls. Doing this you should be able to use your code as is, perhaps a tweak or two for syntax.

your call
Attached Images
File Type: jpg BytesThread_950699_1.jpg (74.0 KB, 7972 views)
Jul 30 '13 #13
tsnave
15
Thank you for all your help. I am going to have to refer to the title of the content control. This project was given to me by a professor who already has 370+ documents already filled out with this type of controls and need the data imported into Access.
Jul 31 '13 #14
zmbd
5,501 Expert Mod 4TB
The title must be UNIQUE!

Two ways to code this (and I will leave the code writing to you :-) )...

One is the select by title method. It is ok to use, but a but fuddy for my taste and if something changes in the document such as the title actually used, then it makes the coding more complex.

Therefore, in this case let's do the loop method as I've already provided in the code Post#10-lines 10 thru 13. and modify the code you posted in post#1.
-->insert and modify your code so that:
-a- Set up variables to hold your values from the documents and for the references you'll need to use the Post#10-lines 10 thru 13. code in your current code module.
-b- Change the "ActiveDocument" object refered to in Post#10-line 10 to your open document object "doc" object refered to on post#1-line 22
-c- Use a select case statement against the value returned by "cc.title" to then return the value from "cc.Range.Text" and assign it to the correct variable that you set up "-a-" keeping in mind that Uppercase and Lowercase are not the same in the comparisons so use something like the lcase() function to format "cc.title" correctly.
-d- make sure you validate your variables either within step -c- or after, if missing or wrong datatype the correct it now.
-e- now take the assigned variables and use those in the code post#1-Lines 32 thru 36
-f- make sure you have the appropiate error traping setup to handle any errors encountered!

Note that in step -c- if you run into a document where the title of the control is slightly off, then you only need to add that variation to the "Case" for the correct variable assignment.

You might also want to modify the If-Then statment regarding the count so that if you do happen to have a document without any content controls you will not receive an error.

Please post back your completed code, even if it works, so that others in this situation can benifit from our efforts.

Given that you have so many documents to handle, you may also wish to take a look at the File open dialog article in our insights section!!!
(*)Microsoft Access / VBA Insights Sitemap \
(*)Article # 29 Select a File or Folder using the FileDialog Object
Jul 31 '13 #15
tsnave
15
Here is my modified code. Works great. I do have 2 checkboxes that I need if they are checked or not.

Expand|Select|Wrap|Line Numbers
  1. Sub GetWordData()
  2.  
  3. Dim cc As ContentControl
  4. Dim fc As Field
  5. Dim ccInfo As String
  6. Dim YourName As String
  7. Dim YourAddress As String
  8. Dim YourPhone As String
  9. Dim Male As CheckBox
  10. Dim Female As CheckBox
  11. Dim appWord As Word.Application
  12. Dim doc As Word.Document
  13. Dim cnn As New ADODB.Connection
  14. Dim rst As New ADODB.Recordset
  15. Dim strDocName As String
  16. Dim blnQuitWord As Boolean
  17.  
  18.  
  19. On Error GoTo ErrorHandling
  20.  
  21.  
  22. strDocName = "C:\SampleForm\" & _
  23.     InputBox("Enter the name of the Word document you want to import:", "Import document")
  24.  
  25.  
  26. Set appWord = GetObject(, "Word.Application")
  27. appWord.Visible = True
  28.  
  29. Set doc = appWord.Documents.Open(strDocName)
  30.  
  31. For Each cc In doc.ContentControls
  32.         ccInfo = "<> ID= " & cc.Id & " Title = " & cc.Title & " Text = " & cc.Range.Text & vbCrLf
  33.         Debug.Print ccInfo
  34.         Select Case cc.Title:
  35.             Case "YourName"
  36.                 YourName = cc.Range.Text
  37.             Case "YourAddress"
  38.                 YourAddress = cc.Range.Text
  39.             Case "YourPhone"
  40.                 YourPhone = cc.Range.Text
  41.             Case "Male"
  42.                 Male = cc.Range.Text
  43.             Case "Female"
  44.                 Female = cc.Range.Text
  45.         End Select
  46.  
  47.  
  48. Next
  49.  
  50.  
  51. Set cnn = CurrentProject.Connection
  52.  
  53. rst.Open "FormData", cnn, adOpenKeyset, adLockOptimistic
  54.  
  55. With rst
  56.     .AddNew
  57.  
  58.  
  59.     rst.Fields("FullName") = YourName
  60.     rst.Fields("Address") = YourAddress
  61.     rst.Fields("Phone") = YourPhone
  62.     rst.Fields("Male") = Male
  63.     rst.Fields("Female") = Female
  64.  
  65.     rst.Update
  66.     .Close
  67. End With
  68.  
  69.  doc.Close
  70.  
  71. If blnQuitWord Then appWord.Quit
  72. cnn.Close
  73. MsgBox "Request Imported"
  74.  
  75. Cleanup:
  76. Set rst = Nothing
  77. Set doc = Nothing
  78. Set appWord = Nothing
  79.  
  80. Exit Sub
  81. ErrorHandling:
  82.  
  83. Select Case Err.Number
  84. Case -2147022986, 429
  85.     Set appWord = CreateObject("Word.Application")
  86.     blnQuitWord = True
  87.     Resume Next
  88.  
  89.  Case 5121, 5174
  90.     MsgBox "You must select a valid Word Document." _
  91.     & "No data imported.", vbOKOnly, _
  92.     " Document Not Found"
  93.  
  94. Case 5491
  95.     MsgBox "The document you selected does not" _
  96.     & " contain the required form fields." _
  97.     & " No data imported.", vbOKOnly, _
  98.     " Fields Not Found"
  99.  
  100. Case Else
  101.     MsgBox Err & ": " & Err.Description
  102.  
  103. End Select
  104. GoTo Cleanup
  105. End Sub
The Male and Female fields are the checkboxes. The other forms that I need to import have around 60 or so checkboxes.
Thank you for all your help. This is my first time with VB and I have learned a lot from all your help.
Jul 31 '13 #16
tsnave
15
I got the checkboxes figured out and here is the finished working code:

Expand|Select|Wrap|Line Numbers
  1. Sub GetWordData()
  2.  
  3. Dim cc As ContentControl
  4. Dim fc As Field
  5. Dim ccInfo As String
  6. Dim YourName As String
  7. Dim YourAddress As String
  8. Dim YourPhone As String
  9. Dim Male As String
  10. Dim Female As String
  11. Dim appWord As Word.Application
  12. Dim doc As Word.Document
  13. Dim cnn As New ADODB.Connection
  14. Dim rst As New ADODB.Recordset
  15. Dim strDocName As String
  16. Dim blnQuitWord As Boolean
  17.  
  18.  
  19. On Error GoTo ErrorHandling
  20.  
  21.  
  22. strDocName = "C:\SampleForm\" & _
  23.     InputBox("Enter the name of the Word document you want to import:", "Import document")
  24.  
  25.  
  26. Set appWord = GetObject(, "Word.Application")
  27. appWord.Visible = True
  28.  
  29. Set doc = appWord.Documents.Open(strDocName)
  30.  
  31. For Each cc In doc.ContentControls
  32.         ccInfo = "<> ID= " & cc.Id & " Title = " & cc.Title & " Text = " & cc.Range.Text & vbCrLf
  33.         Debug.Print ccInfo
  34.         Select Case cc.Title:
  35.             Case "YourName"
  36.                 YourName = cc.Range.Text
  37.             Case "YourAddress"
  38.                 YourAddress = cc.Range.Text
  39.             Case "YourPhone"
  40.                 YourPhone = cc.Range.Text
  41.             Case "Male"
  42.                 If cc.Checked Then
  43.                  Male = "Yes"
  44.                 Else: Male = "No"
  45.                 End If
  46.             Case "Female"
  47.                 If cc.Checked Then
  48.                 Female = "Yes"
  49.                 Else: Female = "No"
  50.                 End If
  51.         End Select
  52.  
  53.  
  54. Next
  55.  
  56.  
  57. Set cnn = CurrentProject.Connection
  58.  
  59. rst.Open "FormData", cnn, adOpenKeyset, adLockOptimistic
  60.  
  61. With rst
  62.     .AddNew
  63.  
  64.  
  65.     rst.Fields("FullName") = YourName
  66.     rst.Fields("Address") = YourAddress
  67.     rst.Fields("Phone") = YourPhone
  68.     rst.Fields("Male") = Male
  69.     rst.Fields("Female") = Female
  70.  
  71.     rst.Update
  72.     .Close
  73. End With
  74.  
  75.  doc.Close
  76.  
  77. If blnQuitWord Then appWord.Quit
  78. cnn.Close
  79. MsgBox "Request Imported"
  80.  
  81. Cleanup:
  82. Set rst = Nothing
  83. Set doc = Nothing
  84. Set appWord = Nothing
  85.  
  86. Exit Sub
  87. ErrorHandling:
  88.  
  89. Select Case Err.Number
  90. Case -2147022986, 429
  91.     Set appWord = CreateObject("Word.Application")
  92.     blnQuitWord = True
  93.     Resume Next
  94.  
  95.  Case 5121, 5174
  96.     MsgBox "You must select a valid Word Document." _
  97.     & "No data imported.", vbOKOnly, _
  98.     " Document Not Found"
  99.  
  100. Case 5491
  101.     MsgBox "The document you selected does not" _
  102.     & " contain the required form fields." _
  103.     & " No data imported.", vbOKOnly, _
  104.     " Fields Not Found"
  105.  
  106. Case Else
  107.     MsgBox Err & ": " & Err.Description
  108.  
  109. End Select
  110. GoTo Cleanup
  111. End Sub
I can't thank you enough for all your help.
Jul 31 '13 #17
zmbd
5,501 Expert Mod 4TB
Happy that we have that working.
I should have also pulled the field type with the code in Post#10 - didn't think of it at the time.

Thank you for posting your final code. I'm sure that alot of people will find this usefull! The built in form for getting information via email leaves much to be desired and if one does not have MS-Infopath installed, then one is stuck with it. By using the word document, the form can be user friendly and with a little code in the controls even the data can be validated.

Two things of note:
1) Post#17:Lines 32 and 33 These two lines are there only for debug work; thus, you can either comment them out or remove them.
2) Using two fields to store the "Sex" information may not be the best idea. "Assuming" that an individual is filling this form out for themself and then you shouls only need one field in the database to store the information.
-- Based on the assumption:
- Modify the data base to have only one field to hold the sex of the respondant.
- Use the exsiting code to pull the information from the check box controls.
- I would then have a little logic to assign either "M," "F," or "X" to a new variable, I would then use this variable to assign the value to the record field and also pop-up an error message that there is a Mutant running loose :)
- You could also log the file name etc... to an errors table.
- With the "X" you can build a query that pulls all records with a Sex = "X" and attempt to connact the respondant.
Then again, the question could be something where checking both boxes is logical... depends on the question; and more than likely still breaks the rules of "normalization"...

...which is something...

You should take a few moments to read about: > Database Normalization and Table Structures.
I'm still looking for a good tutorial site, my favorit went down due to i-frame issues.
Jul 31 '13 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: intl04 | last post by:
Is it possible to create a Word form as the data entry form for an Access database? I didn't see any reference to this possibility in my Access books, so I'm sorry if this is a question that is...
0
by: srkartes | last post by:
Is it possible to have a data entry form in MS Access lookup data for display purposes only. For example, I have error codes associated with specific errors in "Table A" and "Table B" that data is...
2
by: ScardyBob | last post by:
Hello, I am having trouble importing data from an Excel Worksheet. When I try to import the data, everything works except certain columns that contain dates, where it replaces the date with a...
3
by: mukeshsrivastav | last post by:
dear sir i want to move form excel to access. i have 5 excel file having same formats and fields.now i want to import all data in one access table. importing one file is easy .but importing and...
1
by: hexagram | last post by:
hi guys can anybody can teach me how to do this scenario Transfering Data of a form to a new form The Scenario is: I have a Delivery Receipt Form (Hardware) with a fields of the following ...
2
by: Debbiedo | last post by:
I have a text file that I am importing into an Access table that was generatred from data exported from a Word file. Several (about 20-30) fields are from check boxes on the Word form. These fields...
7
by: Rotsey | last post by:
Hi, I am loading a tab control on a form. The code loads textboxes and comboboxes and checkboxes, normal data entry form that loads a table row of data. I have a combo on the form above the...
11
by: Luke Bailey | last post by:
I have a database that I've been building for quite some time. There are a few aspects of the database that I would like to pull over into Excel so that I can do some more advanced pivottable and...
5
by: billa856 | last post by:
Hi I am totaly new to MS Access. I have one worksheet in excel in which I have data of company's inventory. I want to know can I transfer data from a form made in MS Access to the Excel worksheet?...
13
by: BrokenMachine | last post by:
Hi there, I'm using Access 2003 and Word 2003 and I'm trying to import data from the word form to multiple access tables in a one to many relationship, but I can't seem to figure it out. I have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.