Results 1 to 2 of 2

Mail Merge with filtered data

Threaded View

  1. #1
    Registered User
    Join Date
    08-22-2017
    Location
    Belfast
    MS-Off Ver
    MS Office 2013
    Posts
    1

    Mail Merge with filtered data

    Hi Folks, I got the below code from another forum which I have changed to suit and is used to merge data into a word doc.

    What I would hope for now is that if the table is filtered in excel it would only show them records on the word doc when the macro is run as it is currently show all records, could anyone please modify in order for it to work

    Thanks

    Fra
    Sub RunMerge()
    
        Dim wd As Object
        Dim wdocSource As Object
    
        Dim strWorkbookName As String
        
        On Error Resume Next
        Set wd = GetObject(, "Word.Application")
        If wd Is Nothing Then
            Set wd = CreateObject("Word.Application")
        End If
        On Error GoTo 0
    
        Set wdocSource = wd.Documents.Open("I:\Computer Programming\Languages\VBA\Mail Merge\Attendance Registers\Attendance Registers.docm")
    
        strWorkbookName = ThisWorkbook.Path & "\" & ThisWorkbook.Name
    
        wdocSource.MailMerge.MainDocumentType = wdFormLetters
    
        wdocSource.MailMerge.OpenDataSource _
                Name:=strWorkbookName, _
                AddToRecentFiles:=False, _
                Revert:=False, _
                Format:=wdOpenFormatAuto, _
                Connection:="Data Source=" & strWorkbookName & ";Mode=Read", _
                SQLStatement:="SELECT * FROM `Source$`"
    
        With wdocSource.MailMerge
            
            .Destination = wdSendToNewDocument
            .SuppressBlankLines = True
            With .DataSource
                .FirstRecord = wdDefaultFirstRecord
                .LastRecord = wdDefaultLastRecord
            End With
            .Execute Pause:=False
        End With
    
        wd.Visible = True
        wdocSource.Close SaveChanges:=False
    
        Set wdocSource = Nothing
        Set wd = Nothing
    
    End Sub
    Last edited by framcc; 05-24-2018 at 02:50 PM. Reason: Unnecessary code included

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 01-05-2015, 07:35 AM
  2. Replies: 2
    Last Post: 07-12-2012, 08:11 PM
  3. Replies: 0
    Last Post: 04-21-2006, 03:40 PM
  4. [SOLVED] How can I mail merge using filtered data from excel
    By REVA in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-20-2006, 12:10 PM
  5. How do I do mail merge using filtered data
    By REVA in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-19-2006, 02:25 PM
  6. [SOLVED] How can I get Excel filtered data into a word mail merge using a m
    By REVA in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-06-2006, 04:25 PM
  7. [SOLVED] how do i get my mail merge to update the data source at each merge
    By Steel_Monkey in forum Excel General
    Replies: 0
    Last Post: 11-30-2005, 04:45 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1