Create new Word document from template

N

Nigel Blakey

I have an Access 2003 database that lists several hundred standard letters
saved as Word 2003 Templates. Selecting one of the documents in a List Box
will trigger an Event Procedure using the Hyperlink.Follow method. This
starts Word and opens the Word document successfully. But the document is a
Word template (.dot) and I want to create a (.doc) copy of the template
direct from Access.

Does anyone have any ideas?

Thank you
Nigel Blakey
 
O

OssieMac

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top