I found a piece of code here that works great, but have found a little bug. The code is:
Dim dbs As DAO.Database
Dim rstMergeThese As Recordset
Dim oApp As Object
Set oApp = CreateObject("Word.Application")
oApp.Visible = True
With oApp
.Documents.Open "C:\Temp\Service_Plan_Schedule.dot"
' Move to each bookmark and insert text from the form.
'Contract Details
.ActiveDocument.bookmarks("sps_Name").Select
.selection.Text = (CStr(Forms!frmClientDetails!Name))
.ActiveDocument.bookmarks("sps_Address").Select
.selection.Text = (CStr(Forms!frmClientDetails!Address))
.ActiveDocument.bookmarks("sps_HomeTel").Select
.selection.Text = (CStr(Forms!frmClientDetails!Tel_Home))
.ActiveDocument.bookmarks("sps_WorkTel").Select
.selection.Text = (CStr(Forms!frmClientDetails!Work_Home))
'if word isn't running, start and activate it
If Err Then
Shell "C:\Program Files\Microsoft Office\Office\" & "WinWord / Automation", vbMaximizedFocus
AppActivate "Microsoft Word"
End If
' Print the document in the foreground so Microsoft Word 97
' will not close until the document finishes printing.
''.ActiveDocument.PrintOut Background:=False
' Close the document without saving changes.
''.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
' Quit Microsoft Word and release the object variable.
''.Quit
Set oApp = Nothing
End With
When a form is open, you click the cmd button and it copies the fields above to bookmarks I have in word, but the bug I have found is that if the textbox in access does not have any data in it the code stops. Therefore I need some kind of if statement which basically says:
IF Forms!frmClientDetails!Name is BLANK then leave sps_Name Blank
ELSE
.ActiveDocument.bookmarks("sps_Name").Select
.selection.Text = (CStr(Forms!frmClientDetails!Name))
But I am not sure how to write the code.
Dim dbs As DAO.Database
Dim rstMergeThese As Recordset
Dim oApp As Object
Set oApp = CreateObject("Word.Application")
oApp.Visible = True
With oApp
.Documents.Open "C:\Temp\Service_Plan_Schedule.dot"
' Move to each bookmark and insert text from the form.
'Contract Details
.ActiveDocument.bookmarks("sps_Name").Select
.selection.Text = (CStr(Forms!frmClientDetails!Name))
.ActiveDocument.bookmarks("sps_Address").Select
.selection.Text = (CStr(Forms!frmClientDetails!Address))
.ActiveDocument.bookmarks("sps_HomeTel").Select
.selection.Text = (CStr(Forms!frmClientDetails!Tel_Home))
.ActiveDocument.bookmarks("sps_WorkTel").Select
.selection.Text = (CStr(Forms!frmClientDetails!Work_Home))
'if word isn't running, start and activate it
If Err Then
Shell "C:\Program Files\Microsoft Office\Office\" & "WinWord / Automation", vbMaximizedFocus
AppActivate "Microsoft Word"
End If
' Print the document in the foreground so Microsoft Word 97
' will not close until the document finishes printing.
''.ActiveDocument.PrintOut Background:=False
' Close the document without saving changes.
''.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
' Quit Microsoft Word and release the object variable.
''.Quit
Set oApp = Nothing
End With
When a form is open, you click the cmd button and it copies the fields above to bookmarks I have in word, but the bug I have found is that if the textbox in access does not have any data in it the code stops. Therefore I need some kind of if statement which basically says:
IF Forms!frmClientDetails!Name is BLANK then leave sps_Name Blank
ELSE
.ActiveDocument.bookmarks("sps_Name").Select
.selection.Text = (CStr(Forms!frmClientDetails!Name))
But I am not sure how to write the code.