Share via

MS Access mailmerge problem

Anonymous
2024-08-05T19:44:46+00:00

Hi,

I have an Access database which I want to carry out a mailmerge automatically,

I export a table which will only ever contain 1 record at any one time and that record is exported to an xlsx file

I want the database to automatically open the word document and run the mailmerge which has the xlsx file fields set up in it.

The worksheet in the spreadsheet is named Updatedetailsone.

The Word document opens ok but I then get a message box asking which table I want to use Updatedetailsone or Updatedetailsone$.

I want this to be an automatic process which will create the merged Word document.

My code is as follows:

Dim wordApp As Object

Dim wordDoc As Object 

On Error GoTo ErrorHandler 

' Create a new Word application 

Set wordApp = CreateObject("Word.Application") 

wordApp.Visible = True 

' Open the Word template (e.g., "tpl.doc") 

Set wordDoc = wordApp.Documents.Open("C:\xxxxx\Players database\Updates to data\Adult Player Membership Form - textfile.docx") 

' Set up the mail merge data source 

With wordDoc.MailMerge 

    .MainDocumentType = wdMailingLabels 

    .OpenDataSource Name:="C:\xxxxx\Players database\Updates to data\Exportedtable.xlsx", ConfirmConversions:=False, \_ 

        ReadOnly:=False, LinkToSource:=False, AddToRecentFiles:=False 

           .Destination = wdSendToNewDocument 

    .Execute 

    .MainDocumentType = wdNotAMergeDocument 

End With
Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. Anonymous
    2024-08-09T07:40:35+00:00

    Hi again,

    can anyone help with this please as it is now becoming urgent?

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2024-08-07T06:56:48+00:00

    Thanks Sophia for your reply.

    I changed my code to :

    Dim wordDoc As Object 
    
    On Error GoTo ErrorHandler 
    
    ' Create a new Word application 
    
    Set wordApp = CreateObject("Word.Application") 
    
    wordApp.Visible = True 
    
    ' Open the Word template (e.g., "tpl.doc") 
    
    Set wordDoc = wordApp.Documents.Open("C:\xxxxx\Players database\Updates to data\Adult Player Membership Form - textfile.docx") 
    
    ' Set up the mail merge data source 
    
    With wordDoc.MailMerge 
    
        .MainDocumentType = wdMailingLabels 
    
        .OpenDataSource Name:="C:\xxxxx\Players database\Updates to data\Exportedtable.xlsx", \_ 
    
        ConfirmConversions:=False, \_ 
    
        ReadOnly:=False, \_ 
    
        LinkToSource:=False, \_ 
    
        AddToRecentFiles:=False, \_ 
    
        Connection:="Updatedetailsone" 
    
        ' Specify other mail merge settings (e.g., SQLStatement) 
    
        ' ... 
    
        .Destination = wdSendToNewDocument 
    
        .Execute 
    
        .MainDocumentType = wdNotAMergeDocument 
    
    End With 
    

    ErrorHandler:

    ' Handle any errors

    MsgBox "Error: " & Err.Description

    ' Clean up 
    
    wordDoc.Close False 
    
    Set wordDoc = Nothing 
    
    Set wordApp = Nothing 
    
    Exit Sub
    

    The code is opening the Word template and then displays this error:

    Image

    Updatedetailsone is the worksheet name in the spreadsheet Exportedtable.xlsx.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2024-08-06T12:14:45+00:00

    Hi StormingNorman41,

    Kindly add 'Connection' argument to MailMerge.OpenDataSource method.

    This argument retrieves data from the named range:

    With wordDoc.MailMerge
        ' ... some code
        .OpenDataSource Name:="C:\xxxxx\Players database\Updates to data\Exportedtable.xlsx", _
                        ConfirmConversions:=False, _
                        ReadOnly:=False, _
                        LinkToSource:=False, _
                        AddToRecentFiles:=False, _
                        Connection:="Updatedetailsone"
        ' ... some code
    End With
    

    MailMerge.OpenDataSource method (Word):

    https://learn.microsoft.com/en-us/office/vba/api/word.mailmerge.opendatasource?f1url=%3FappId%3DDev11IDEF1%26l%3Dzh-CN%26k%3Dk(vbawd10.chm153092208)%3Bk(TargetFrameworkMoniker-Office.Version%3Dv16)%26rd%3Dtrue 

    Best Regards,

    Sophia

    Was this answer helpful?

    0 comments No comments