Results 1 to 4 of 4
  1. #1
    scott0_1 is offline Advanced Beginner
    Windows Vista Access 2013 32bit
    Join Date
    Apr 2018
    Posts
    82

    Error while connecting a Word Mail Merge to an Access query

    I have a database of training courses and I want to be able to email all enrolled participants invitations. I have a query that shows all participants enrolled in a specific session. The query works fine when run from a form in Access.
    Here is the code I'm using for the mail merge:


    Code:
    
    
    Code:
        Dim oWord As Word.Application    Dim oWdoc As Word.Document
        Dim wdInputName As String
        Dim wdOutputName As String
        Dim outFileName As String
    
    
        ' Set Template Path
        '------------------------------------------------
        wdInputName = CurrentProject.Path & "\MailMergeDocs\CatIII.docx"
    
    
        ' Create unique save filename with minutes
        ' and seconds to prevent overwrite
        '------------------------------------------------
        outFileName = "MailMergeFile_" & Format(Now(), "yyyymmddmms")
    
    
        ' Output File Path w/outFileName
        '------------------------------------------------
        wdOutputName = CurrentProject.Path & "\completed\" & outFileName
    
    
        Set oWord = New Word.Application
        Set oWdoc = oWord.Documents.Open(wdInputName)
    
    
        ' Start mail merge
        '------------------------------------------------
        With oWdoc.MailMerge
            .MainDocumentType = wdFormLetters
            .OpenDataSource _
                Name:=CurrentProject.FullName, _
                AddToRecentFiles:=False, _
                LinkToSource:=True, _
                Connection:="QUERY qryFirstAidMailMerge", _
                SQLStatement:="SELECT * FROM [qryFirstAidMailMerge]"
            .Destination = wdSendToEmail
            .MailAsAttachment = False
            .MailFormat = wdMailFormatHTML
    
    
            .Execute Pause:=False
        End With
    
    
        ' Hide Word During Merge
        '------------------------------------------------
        oWord.Visible = False
    
    
        ' Quit Word to Save Memory
        '------------------------------------------------
        oWord.Quit savechanges:=False
    
    
        ' Clean up memory
        '------------------------------------------------
        Set oWord = Nothing
        Set oWdoc = Nothing

  2. #2
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    That first section of code is certainly concise
    Do you get an error? If so, what and where?
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  3. #3
    scott0_1 is offline Advanced Beginner
    Windows Vista Access 2013 32bit
    Join Date
    Apr 2018
    Posts
    82
    Oops... Fixed the formatting of my post and deleted the important part!!!!

    When I ran this code with my query setup statically, it work perfectly. I then changed the query to grab criteria from the user form. Once I did that, many things went wrong. The code freezes and I need to open Task Manager to end a few processes including ODBC Microsoft Access Driver Login Failed.
    When I went into the Word document to change the format of a few things, I tried running the mail merge from there. I got a few errors, so I tried to check the connection settings. When I try to pick the table/query to get the data from, my saved query is no longer visible. I assume that is because it depends on variables.
    The final issue I'm having is that on occasion, I get an error that says that user 'admin' has the database locked on machine XXXX. Again, I wasn't getting this issue earlier, so I'm not sure why I'm getting this now.
    I haven't included the saved query as it works fine, but let me know if it is needed to solve this and I'll update.
    The questions I have are:

    1. What would cause the ODBC error
    2. Why is the Access query not visible in the "Select Table" dialog box in Word?
    3. What would cause the database to be locked when it is trying to run the mail merge?

  4. #4
    scott0_1 is offline Advanced Beginner
    Windows Vista Access 2013 32bit
    Join Date
    Apr 2018
    Posts
    82
    I solved this issue by creating a temporary table before running the mail merge. The word mail merge really does not like variables! And I think the ODBC error was happening because I wasn't pointing the merge to anything. Here is the code I used to solve it. I wanted to give credit to the person who posted it on stackoverflow.com, but I can't find the post right now.

    Code:
    Private Sub TempTable()
        Const newtbl As String = "tblTempTable" 'whatever you want to call it
        Dim var As Variant
            
        Dim db As DAO.Database, td As DAO.TableDef
        Set db = CurrentDb
        On Error Resume Next
        
        'see if table already exists
        Set td = db.TableDefs(newtbl)
        If Err.Number = 0 Then
            'table exists. delete it
            DoCmd.DeleteObject acTable, newtbl
            If Err.Number <> 0 Then
                'failed to delete the mofo :(
                MsgBox Err.Description
                Exit Sub
            End If
        Else
            Err.Clear
        End If
        
        On Error GoTo 0
         
        DoCmd.SetWarnings (False)
    
            DoCmd.RunSQL "SELECT * INTO " & newtbl & " FROM qryQuery" 'Replace with your query name
    
            
        DoCmd.SetWarnings (True)
        Set db = Nothing
        Set td = Nothing
        
        RefreshDatabaseWindow
    End Sub

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 08-13-2015, 11:20 AM
  2. Replies: 1
    Last Post: 07-18-2015, 08:02 AM
  3. Mail merge from Access to Word
    By Matthew7 in forum Access
    Replies: 1
    Last Post: 02-18-2015, 07:46 AM
  4. Error when creating mail merge doc or any word doc
    By maxmaggot in forum Programming
    Replies: 5
    Last Post: 08-25-2013, 01:35 PM
  5. Access Query mail merge to Word
    By Jan Collier in forum Access
    Replies: 8
    Last Post: 08-30-2010, 09:52 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums