473,385 Members | 1,523 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,385 software developers and data experts.

Button to fill Word Document

I'm trying to create a button to fill a word document with information from access. here is my code, can someone tell me what I have done wrong? When I click the button it gives me an error on the Private Sub fillwordform() and Dim appword as word.application.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command170_Click()
  2. Call fillwordform
  3. End Sub
  4.  
  5. Private Sub fillwordform()
  6. Dim appword As Word.Application
  7. Dim doc As Word.Document
  8. Dim path As String
  9.  
  10. On Error Resume Next
  11. Error.Clear
  12. path = "C:\Users\gary.milam\Desktop\Nom Letters\Nomletter.docx"
  13. Set appword = GetObject(, "word.application")
  14. If Err.Number <> 0 Then
  15. Set appword = New Word.Application
  16. appword.Visible = True
  17. End If
  18.  
  19. Set doc = appword.Documents.Open(path, , True)
  20. With doc
  21.     .formfields(txtLastName).results = Me.LastName
  22.     .formfields(txtFirstName).results = Me.FirstName
  23.     .formfields(txtMiddleInitial).results = Me.MiddleInitial
  24.     .formfields(txtSSN).results = Me.SSN
  25. End With
  26.  
  27. appword.Visible = True
  28. appword.Activate
  29.  
  30. Set doc = Nothing
  31. Set appword = Nothing
  32.  
  33.  
  34. End Sub
  35.  
May 9 '16 #1
4 1691
So I got it to open the word document, but its not filling in the form fields on the word document. Help anyone.
May 9 '16 #2
jforbes
1,107 Expert 1GB
First thing you might want to do is to read through this: Early vs Late Binding.

Hopefully, after that reading you will notice you are attempting to open Word with both Early (line 15) and Late (Line 13) bindings. You really will only want to use one of those lines. There are pros and cons to either approach. I personally go with the Early Binding as it enables Intellisense which makes it easier to develop the code.

If you use Early Binding, you need a reference to Word. This is what usually trips up most people. From the VB Editor menu, select Tools and then References. Then scroll down till you see something like Microsoft Word X.0 Object Library and tick the Checkbox.

Getting this nailed down should make it easier to troubleshoot your code.
May 9 '16 #3
ADezii
8,834 Expert 8TB
A couple of minor changes along with Early Binding will do the trick:
Expand|Select|Wrap|Line Numbers
  1. 'Automation Code to fill Form Fields in a Microsoft Word Document
  2. 'from within an Access Database. Don't forget to
  3. 'Set a Reference to the Microsoft Word XX.X Object Library
  4. Dim appword As Word.Application
  5. Dim doc As Word.Document
  6. Dim path As String
  7.  
  8. On Error Resume Next
  9. Error.Clear
  10.  
  11. path = "C:\Test\Test.docx"
  12.  
  13. Set appword = New Word.Application
  14.  
  15. appword.Visible = True
  16.  
  17. Set doc = appword.Documents.Open(FileName:=path, ReadOnly:=False)
  18.  
  19. With doc
  20.   .FormFields("txtLastName").Result = "Smith"
  21.   .FormFields("txtFirstName").Result = "Joseph"
  22.   .FormFields("txtMiddleInitial").Result = "K"
  23.   .FormFields("txtSSN").Result = "999-99-9999"
  24. End With
  25.  
  26. Set doc = Nothing
  27. Set appword = Nothing
May 9 '16 #4
Rabbit
12,516 Expert Mod 8TB
What about a mail merge?
May 10 '16 #5

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

Similar topics

4
by: Tom Dauria | last post by:
What I am trying to do is write a resume into a word document from information in an Access database. I have been using bookmarks and inserting table results into the document and so far it's...
1
by: Zameer Syed | last post by:
I have a webform in which users enter their information and there is a button at the end of form,which when clicked should open a word document with fields filled in dynamically with the...
4
by: Charlie Brookhart | last post by:
I am trying to write a code for a button click event. When the button is clicked, it is supposed to bring up an open file dialog box to allow the user to select the document they which to open....
2
by: jmar | last post by:
I am updating a VB4.0 quote generation program to VB.net. The old program takes user inputs, performs calculations, saves the data to Access databases and uses Crystal Reports 5.0 to generate a...
0
southoz
by: southoz | last post by:
Good ay all , I'm fairly new to access(a little over 5 weeks now). Since I'v started I have picked up a lot of useful information from forums such as this and in doing so will share that information...
17
by: wparrott | last post by:
Hello experts! I'm having trouble coding a button on a form and could use some help. In a nutshell, I'm attempting to code the button to do the following: 1. Choose the appropriate Word...
7
by: Peter | last post by:
ASP.NET 2.0 I am trying to open a Word document and Excel document from a dialog web page, what's the best way to do that? I have tried the following: Response.Clear();...
3
by: boyindie86 | last post by:
Hi Forum, I have been trying to create an automated report in word, using VB.NET. I am trying to build a program, which will read an XML template of the word document, which should traverse...
0
by: Lyu KingT | last post by:
hi, i need to control the right of a Office document in my Visual Studio Projects, so how to enable or disable the print, what abou edit, save..... i try to fellow the video How Do I: Hi-jack...
1
by: Ghaleon | last post by:
I create a blank document like this: Private Word_App As Word.Application Private Word_Doc As Word.Document Private Word_Rtf_Doc As...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.