Hi
I have some code that was working quite well - that essentially fills in some data stored in Access into a Word document using Bookmarks. This was all working fine, now I want to change it to not just use one Letter/Document the path for which I had hardcoded for testing but instead the user will select the Letter from a drop down list in Access. My problems are around LetterPath and LetterPath2.
When I run it at the moment I get and "Object required" error and the Set Letter path line is highlighted - if I remove the word Set this bit seems to be ok but fails when it is trying to open the Document.
Thanks,
MissNiffy
Private Sub Letter_Click()
Dim wApp As Word.Application
Dim wDoc As Word.Document
Dim rs As DAO.Recordset
Dim query As QueryDef
Dim JobID As Variant
Dim LetterName As Variant
Dim LetterPath As Variant
Dim LetterPathNew As Variant
Set JobID = [Form_JOBS SUB].[JOBS.ID]
Set LetterName = [Form_JOBS SUB].Letter
Set LetterPath = "C:\temp\Letters\Install.docx"
Set query = CurrentDb.QueryDefs("Letters")
query!JobID2 = JobID
Set rs = query.OpenRecordset(dbOpenDynaset, dbSeeChanges)
Set wApp = New Word.Application
Set wDoc = wApp.Documents.Open(LetterPath)
If Not rs.EOF Then rs.MoveFirst
Do Until rs.EOF
wDoc.Bookmarks("FirstName").Range.Text = Nz(rs!FirstName, "")
wDoc.Bookmarks("Surname").Range.Text = Nz(rs!Surname, "")
wDoc.Bookmarks("PropertyNo").Range.Text = Nz(rs!PropertyNo, "")
wDoc.Bookmarks("PropertyName").Range.Text = Nz(rs!PropertyName, "")
wDoc.Bookmarks("Road").Range.Text = Nz(rs!Road, "")
wDoc.Bookmarks("Town").Range.Text = Nz(rs!TOWN, "")
wDoc.Bookmarks("County").Range.Text = Nz(rs!COUNTY, "")
wDoc.Bookmarks("Postcode").Range.Text = Nz(rs!Postcode, "")
wDoc.Bookmarks("InstallationDate").Range.Text = Nz(rs!InstallationDate, "")
wDoc.Bookmarks("InstallationTime").Range.Text = Nz(rs!InstallationTime, "")
wDoc.Bookmarks("Duration").Range.Text = Nz(rs!Duration, "")
wDoc.Bookmarks("Client").Range.Text = Nz(rs!Client, "")
wDoc.Bookmarks("PropertyNo2").Range.Text = Nz(rs!PropertyNo, "")
wDoc.Bookmarks("PropertyName2").Range.Text = Nz(rs!PropertyName, "")
wDoc.Bookmarks("Road2").Range.Text = Nz(rs!Road, "")
Set LetterPath2 = "C:\temp\letters\output" & rs!Id & "_" & LetterName & ".docx"
wDoc.SaveAs2 LetterPath2
rs.MoveNext
Loop
wApp.Application.Visible = True
wApp.Documents.Open LetterPath2
End Sub