Word Mail Merge Bookmark Problem IF Problem (1 Viewer)

danian

Registered User.
Local time
Today, 21:12
Joined
Jun 27, 2005
Messages
54
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.
 

alastair69

Registered User.
Local time
Today, 14:12
Joined
Dec 21, 2004
Messages
562
Do you have a copy of Service_Plan_Schedule.dot file so i can have a look for you.
 

jubb

Registered User.
Local time
Tomorrow, 08:12
Joined
Jul 27, 2005
Messages
50
Try this out, it should do what you want

Code:
if isnull(Forms!frmClientDetails!Name) then 
'i usually display a message here asking the user to enter the missing data
'then set my controls and objects to nothing and exit the sub
else
' i also add code here to check if the bookmark exists in the document 
template
if .activedocument.bookmark.exists("sps_Name") then
.ActiveDocument.bookmarks("sps_Name").Select
.selection.Text = (CStr(Forms!frmClientDetails!Name))
end if
end if

Let me know if you have any probs.

Cheers

jubb
 

Users who are viewing this thread

Top Bottom