Results 1 to 7 of 7

Thread: [RESOLVED] Mail merge problem looping through records set

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    7

    Resolved [RESOLVED] Mail merge problem looping through records set

    I need to create individual merge documents from Access and then attach each document to an email sent from Access. All works fine providing I do them one at a time. However, when I loop through a record set to send each one seperately, the first record goes through fine but then I get an error message "The remote server machine does not exist or is unavailable".

    This is what I have so far

    For Each varItem In ctlList.ItemsSelected
    intMemberID = ListMFD.Column(1, varItem)
    strEmailAddress = ListMFD.Column(6, varItem)
    strContactName = ListMFD.Column(2, varItem)
    strFee = "£" & ListMFD.Column(5, varItem) & ".00"
    strMemNo = ListMFD.Column(3, varItem)
    DteDue = ListMFD.Column(4, varItem)

    'create mail merge reminder docs

    Set objDoc = objWord.Documents.Open(strTemplatePath & "\RenewalMailMerge.doc")

    objDoc.MailMerge.OpenDataSource Name:="", _
    Connection:="DSN=MMSSQL;DATABASE=MMSSQL;uid=SWCAAMMS;pwd=swcaamms;", _
    SQLStatement:="SELECT ShortName, MembershipNo, FeeNextDue " & _
    "FROM VW_MembersFeeRemindersDueMM WHERE MembershipNo = '" & strMemNo & "'", _
    LinkToSource:=True, AddToRecentFiles:=False
    objDoc.MailMerge.Destination = wdSendToNewDocument
    objDoc.MailMerge.Execute
    objDoc.Close
    objWord.Documents(1).SaveAs (strSavedDocsPath & "\" & strMemNo & "_" & Format(Now(), "yyyy") & "_Renewal.doc")
    objWord.Documents.Close
    objWord.Quit (Word.wdSaveChanges = False)
    Next varitem

    Can anyone help please.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Mail merge problem looping through records set

    objWord.Quit (Word.wdSaveChanges = False)
    as you close word here it is no longer available to open the next document, only do this after finishing all documents

    also you are closing the document, before the saveAs, which as you want to use it again, seems to be incorrect (inefficient), you only want to close the new document
    Last edited by westconn1; Mar 16th, 2010 at 04:00 PM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    7

    Re: Mail merge problem looping through records set

    Thanks Pete. Your a star. Any ideas on how you turn off outlook warnings when sending email as part of the same process. I am attaching each document to an email and sending them from the app but get the message "Aprogram is trying to send email on your behalf etc"?

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Mail merge problem looping through records set

    robdog has posted some code to close outlook security warnings, but you would need to search for it, it may be in the faq thread at top of this forum

    otherwise use findwindow and sendmessage APIs
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    7

    Re: Mail merge problem looping through records set

    Thanks Pete, have searched everywhere but can't find it. Can you point me in the right direction to robdog's post please

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    7

    Re: Mail merge problem looping through records set

    Thanks Pete, that will do the job nicely.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width