Help in having a word doc autofill on opening from access (1 Viewer)

Nicole-B

New member
Local time
Today, 15:40
Joined
Jan 18, 2021
Messages
3
Hi Guys

I am having a bit of trouble trying to get a Word Document to open from access and autofill with the information from a corresponding query.

So basically what i have is a client database and when I have a particular clients record open i want to be able to date a box and the word document open up and automatically fill with the information pertaining to that client that is stored on a query. I dont need it to save the file or anything like that. Just open a completed form so we can print it and close it again.

I have managed to connect the word document to the datebox and have it open. but i cant get the information to fill in.

Im pretty new to Visual Basics and trying to update a database that someone else created long ago so have used a tutoral online to create this (be kind to me)

I have followed the instructions for inserting bookmarks into the word document and then wrote the code in visual basic but i keep getting the error "The Range Cannot Be Deleted"

Can some one help me figure out where i am going wrong please?

Code is as follows:

Sub F4FundingReport()

Dim wApp As Word.Application
Dim wDoc As Word.Document
Dim rs As DAO.Recordset

Set wApp = Word.Application
Set wDoc = wApp.Documents.Open("\\knetapp1b\data\Care And Repair\Aids and Adaptations 2010\Word\F4 Funding Report Jan21.doc")
Set rs = CurrentDb.OpenRecordset("QRY_F4FundingReport")

If Not rs.EOF Then rs.MoveFirst

Do Until rs.EOF
wDoc.Bookmarks("ReferralID").Range.Text = (rs!ReferralID)
wDoc.Bookmarks("Title").Range.Text = (rs!Client1Title)
wDoc.Bookmarks("FirstName").Range.Text = (rs!Client1Forename)
wDoc.Bookmarks("Surname").Range.Text = (rs!Client1Surname)
wDoc.Bookmarks("Address1").Range.Text = (rs!ClientAddress1)
wDoc.Bookmarks("Address2").Range.Text = (rs!ClientAddress2)
wDoc.Bookmarks("Town").Range.Text = (rs!ClientTown)
wDoc.Bookmarks("County").Range.Text = (rs!ClientCounty)
wDoc.Bookmarks("Postcode").Range.Text = (rs!ClientPostcode)
wDoc.Bookmarks("OTWorksDescription").Range.Text = (rs!OTWorksDescription)
wDoc.Bookmarks("F4TotalCosts").Range.Text = (rs!F4TotalWorksQuote)
wDoc.Bookmarks("AnticipatedGrant").Range.Text = (rs!AnticipatedGrant%)
wDoc.Bookmarks("EstimatedGrantAmount").Range.Text = (rs!EstimatedGrantAward£)
wDoc.Bookmarks("AnticipatedBTS").Range.Text = (rs!AnticipatedBTSAward%)
wDoc.Bookmarks("EstimatedBTSAmount").Range.Text = (rs!EstimatedBTSAward£)
wDoc.Bookmarks("TitleFees1").Range.Text = (rs!TitleSearchFee)
wDoc.Bookmarks("Contractor1").Range.Text = (rs!Contractor)
wDoc.Bookmarks("Titlefee2").Range.Text = (rs!TitleSearchFee)
wDoc.Bookmarks("ClientShare1").Range.Text = (rs!ClientShare)
wDoc.Bookmarks("ClientShare2").Range.Text = (rs!ClientShare)
wDoc.Bookmarks("Contractor2").Range.Text = (rs!Contractor)

Loop

End Sub
 

Dreamweaver

Well-known member
Local time
Today, 15:40
Joined
Nov 28, 2005
Messages
2,466
I think The word doc needs to be saved as a template but haven't worked with word automation in years.
 

ypma

Registered User.
Local time
Today, 15:40
Joined
Apr 13, 2012
Messages
643
Nicol, have you considered using mail merge , your query as the data source, which could be filtered to show the current client . The word template could be used each time showing the details of the appropriate client. I notice you are using an old version of word and you might have to disable the security prompt. via the Registry Editor .

I am a user not a professional and hope this is of use to you.

Regards Ypma
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 11:40
Joined
Feb 19, 2002
Messages
42,970
Given your description, you don't need a loop. Use a query with criteria that selects ONE record. Get rid of the Loop. There is also no moveNext inside the loop so you wouldn't ever move off the first record anyway. I've attached an example that might help.
 

Attachments

  • SampleWordAutomation200919.zip
    300.2 KB · Views: 390

Users who are viewing this thread

Top Bottom