By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,506 Members | 2,311 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,506 IT Pros & Developers. It's quick & easy.

Mail Merge Problem

P: n/a
I am having problems closing winword.exe.My mail merge works ok but the word
application is not closing down. Can anyone help in telling me where I am
going wrong and what I am missing out of my code. Ihave had suggestions but
nothing as worked up to now.
Many thanks
Dave
Public Sub Fire()
'On Error GoTo Err_Fire
Dim db As Database
Dim rst As Recordset
'Dim olkApp As Outlook.Application
'Dim objmailitem As Outlook.MailItem
Dim objscript
'Dim SafeItem
'Dim utils
'Dim strAttach As String
'Dim strfilename As String
Dim objWord As Word.Document

Set objscript = CreateObject("Scripting.FileSystemObject")
If Not objscript.FolderExists("D:\" & "Fire\") Then
objscript.createfolder ("D:\" & "Fire\")
End If
If Dir("D:\Fire\*.doc") <> "" Then
Kill "D:\Fire\*.doc"
End If
Set objscript = Nothing
'On Error Resume Next
Set db = CurrentDb
Set rst = db.OpenRecordset("tblrelmanager")

Set objWord = GetObject("C:\Documents and Settings\David\Desktop\fire
risk assessment.doc", "Word.Document")

' Make Word visible.

objWord.Application.Visible = True
Do Until rst.EOF

' Set the mail merge data source as the WSS database.
objWord.MailMerge.OpenDataSource _
Name:="C:\Documents and Settings\David\Desktop\WSS.mdb", _
LinkToSource:=True, _
Connection:="table qryAccenture", _
SQLStatement:="SELECT * FROM [qryAccenture] WHERE [Relationship
Manager] = '" & rst![Relationship Manager] & "'"

' Execute the mail merge.
objWord.MailMerge.Execute

ActiveDocument.SaveAs FileName:="D:\fire\" & rst![Relationship Manager]
ActiveDocument.Close
rst.MoveNext
Loop

'objWord.Application.Quit False
objWord.Close
Set objWord = Nothing

rst.Close
Set rst = Nothing
db.Close
Set db = Nothing

DoCmd.SetWarnings True


Exit_Fire:
Exit Sub

Err_Fire:
MsgBox Err.Description
Resume Exit_Fire

End Sub

Nov 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
where's the problem closing Word? You never explicitly close Word in
your code. Did you want it to close if it wasn't open when you started
(when GetObject is called)? If so, you could use fIsAppRunning from
AccessWeb and store it somewhere.

dim blnIsRunning as Boolean
blnIsRunning=fIsAppRunning("Word") '<--I'm guessing about the syntax
....do your thing...

Then
if not blnIsRunning Then '---Word was not running
appWord.Quit
End If

Nov 13 '05 #2

P: n/a
It would be polite to mention that you are asking elsewhere
(microsoft.public.access.modulesdaovba) so that multiple people are not
working on this, each unaware that others are duplicating the work.

"David" <da***********@btinternet.com> wrote in message
news:dc**********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com...
I am having problems closing winword.exe.My mail merge works ok but the
word
application is not closing down. Can anyone help in telling me where I am
going wrong and what I am missing out of my code. Ihave had suggestions
but
nothing as worked up to now.
Many thanks
Dave
Public Sub Fire()
'On Error GoTo Err_Fire
Dim db As Database
Dim rst As Recordset
'Dim olkApp As Outlook.Application
'Dim objmailitem As Outlook.MailItem
Dim objscript
'Dim SafeItem
'Dim utils
'Dim strAttach As String
'Dim strfilename As String
Dim objWord As Word.Document

Set objscript = CreateObject("Scripting.FileSystemObject")
If Not objscript.FolderExists("D:\" & "Fire\") Then
objscript.createfolder ("D:\" & "Fire\")
End If
If Dir("D:\Fire\*.doc") <> "" Then
Kill "D:\Fire\*.doc"
End If
Set objscript = Nothing
'On Error Resume Next
Set db = CurrentDb
Set rst = db.OpenRecordset("tblrelmanager")

Set objWord = GetObject("C:\Documents and Settings\David\Desktop\fire
risk assessment.doc", "Word.Document")

' Make Word visible.

objWord.Application.Visible = True
Do Until rst.EOF

' Set the mail merge data source as the WSS database.
objWord.MailMerge.OpenDataSource _
Name:="C:\Documents and Settings\David\Desktop\WSS.mdb", _
LinkToSource:=True, _
Connection:="table qryAccenture", _
SQLStatement:="SELECT * FROM [qryAccenture] WHERE [Relationship
Manager] = '" & rst![Relationship Manager] & "'"

' Execute the mail merge.
objWord.MailMerge.Execute

ActiveDocument.SaveAs FileName:="D:\fire\" & rst![Relationship Manager]
ActiveDocument.Close
rst.MoveNext
Loop

'objWord.Application.Quit False
objWord.Close
Set objWord = Nothing

rst.Close
Set rst = Nothing
db.Close
Set db = Nothing

DoCmd.SetWarnings True


Exit_Fire:
Exit Sub

Err_Fire:
MsgBox Err.Description
Resume Exit_Fire

End Sub

Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.