Sub ExportToWord()
Dim rs As Recordset
Dim fld As Field
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim strBookmarkName As String
On Error Resume Next
Set wrdApp = GetObject(, "Word.Application")
On Error GoTo 0
If wrdApp Is Nothing Then 'word wasn't already running, so create a new instance
Set wrdApp = CreateObject("Word.Application")
End If
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Add("C:MyTemplates\MyTemplate.dot")
Set rs = Form1.Recordset
For Each fld In rs.Fields
strBookmarkName = Replace(fld.Name, " ", "_") 'bookmark names must not contain spaces
wrdDoc.Bookmarks(strBookmarkName).Range.Text = fld.Value
Next fld
End Sub
|