Hi Nigel,
This bit of code is out of one of my projects that I got help from this
forum with. It creates a word doc from a template and then copies data to
fields. I have left in the code to copy data to fields because you might also
find that helpful.
Note: The word document is not saved with this code so will need
modification of this line
.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
if you want to save it.
Private Sub PrintWordDoc()
Dim objWord As Object
Dim strPathFileName As String
Dim i As Long
'Save the Forms textbox values to variables
'using space in lieu of null values.
'(Error produced on Word doc if zero length string
'is used in lieu of null value; hence use of space if Null)
strReceiptNo = Nz(Me.ReceiptNo, " ")
strOrganizationName = Nz(Me.EROrganizationPost, " ")
strAddr1 = Nz(Me.ErAddrPost1, " ")
strAddr2 = Nz(Me.ERAddrPost2, " ")
strLocal = Nz(Me.ERLocalityPost, " ")
strState = Nz(Me.ERStatePost, " ")
strPostCode = Nz(Me.ERPostCodePost, " ")
Set objWord = CreateObject("Word.Application")
'Save required path and filename of template file
strPathFileName = CurDir & "\" & "ERLA Template.dot"
With objWord
'Create a new Word document based on the template
.documents.Add template:=strPathFileName
.Visible = False 'Can be True
.ActiveDocument.Variables("RecptNo").Value = strReceiptNo
.ActiveDocument.Variables("EligRecipient").Value = strOrganizationName
.ActiveDocument.Variables("PostalAddress").Value = _
Trim(strAddr1 & " " & strAddr2 & " " & strLocal & " " _
& strState & " " & strPostCode)
.ActiveDocument.Fields.Update
If .ActiveWindow.View.ShowFieldCodes = True Then
.ActiveWindow.View.ShowFieldCodes = False
End If
.ActiveDocument.PrintOut
.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
.Quit
End With
set objWord = Nothing
Exit Sub