Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / July 2005

Tip: Looking for answers? Try searching our database.

Mail Merge

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
david.roebuck@btinternet.com - 24 Jul 2005 14:06 GMT
I hope someone can help me with this,I am trying to create a mail merge
from within my access database. The source of the data is a table which
I want to create mail merges based on a field name called Relationship
Manager. I want to open the word doc template and create the mail merge
then save it to a file then create the next mail merge and save that
etc.
The problem I am having is linking to the data source as it tries to
open the database again and also closing the word application
afterwards.
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")

Do Until rst.EOF

   Set objWord = GetObject("C:\Documents and
Settings\David\Desktop\fire risk assessment.doc", "Word.Document")
  ' Make Word visible.
  objWord.Application.Visible = True
  ' 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 tblAccenture", _
     SQLStatement:="SELECT * FROM [tblAccenture] 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
   ActiveDocument.Close
   objWord.Quit
   rst.Close
   Set rst = Nothing
   db.Close
   Set db = Nothing
Thanks in advance     Dave
Justin Hoffman - 25 Jul 2005 10:44 GMT
>I hope someone can help me with this,I am trying to create a mail merge
> from within my access database. The source of the data is a table which
[quoted text clipped - 64 lines]
>    Set db = Nothing
> Thanks in advance     Dave

There are a number of things I could comment on here: why not set a
Word.Application object outside the loop? what are the Outlook objects for?
the error handling should be improved, etc
But for an immediate solution to stop another session of Access starting,
why not export the query (DoCmd.TransferText) as a temporary text file and
use that as the source for the mailmerge.  I use that approach for a number
of reasons including overcoming user-level security complications.
If Word is not quitting you need to revise the routine (including error
handling) to make sure that you dispose with Word objects properly.  If you
remove the On Error Resume Next line from your code, you may find you are
ignoring errors you should be dealing with e.g. ActiveDocument.Close.  The
'On Error Resume Next' bit should only be used for a single line, or perhaps
a cleanup part at the end.  As mentioned above, I would have a
Word.Application object so the final cleanup part might be:

Exit_Routine:
   On Error Resume Next
   If Not wdDoc Is Nothing Then
       wdDoc.Close
       Set wdDoc=Nothing
   End If
   If Not wdApp Is Nothing Then
       wdApp.Quit
       Set wdApp=Nothing
   End If
   Exit Sub
David - 25 Jul 2005 23:36 GMT
Please help, I still cannot drop winword on closure. Do I need to add
anything to my code? The mail merge works but winword.exe does not close.

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

>>I hope someone can help me with this,I am trying to create a mail merge
>> from within my access database. The source of the data is a table which
[quoted text clipped - 91 lines]
>    End If
>    Exit Sub
Justin Hoffman - 26 Jul 2005 00:16 GMT
> Please help, I still cannot drop winword on closure. Do I need to add
> anything to my code? The mail merge works but winword.exe does not close.
>
> Many thanks
>
> Dave

Did you understand the advice about creating a Word.Application object?
Anyway, there are still a couple of decisions you need to make before you
start any coding.  What happens if the user has Word open anyway?  My advice
would be for your code to check whether Word is running - if it is you can
use that instance - just don't close it because you didn't open it and that
might irritate the user who did.  If your code did open it, then you should
do your best to close it afterwards.

> Public Sub Fire()
>
[quoted text clipped - 199 lines]
>>    End If
>>    Exit Sub
david.roebuck@btinternet.com - 26 Jul 2005 13:58 GMT
I have decided to start again, as I am having so many problems. Using
your idea to 'TransferText' to a text file. I am going to take this one
step at a time. After creating this file how do I open it? Do I still
use ......objWord.MailMerge.OpenDataSource and if so how do i reference
it
Justin Hoffman - 26 Jul 2005 15:52 GMT
>I have decided to start again, as I am having so many problems. Using
> your idea to 'TransferText' to a text file. I am going to take this one
> step at a time. After creating this file how do I open it? Do I still
> use ......objWord.MailMerge.OpenDataSource and if so how do i reference
> it

Yes - look under the Word help file for OpenDataSource.  You will see that
you should let the mailmerge know that plain text is used as the datasource.
WdOpenFormat constant is wdOpenFormatText (=4).

wdDoc.MailMerge.OpenDataSource _
       Name:="C:\MyText.txt", _
       Format:=4, _
       ReadOnly:=True, _
       LinkToSource:=False, _
       AddToRecentFiles:=False

If you would like, and I can find some time, I could send you an example.
david.roebuck@btinternet.com - 26 Jul 2005 19:36 GMT
Thanks Justin I would appreciate that if you can spare the time
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.