View Single Post
 
Old 03-15-2018, 02:40 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try something based on:
Code:
Sub Demo()
Dim strWkBkNm As String
With Application
  .DisplayAlerts = wdAlertsNone
  'Select a data source
  With .FileDialog(FileDialogType:=msoFileDialogFilePicker)
    .AllowMultiSelect = False
    .Filters.Add "Excel Files", "*.xls*", 1
    If .Show = True Then
      strWkBkNm = .SelectedItems(1)
    Else
      Exit Sub
    End If
  End With
  With .ActiveDocument
    With .MailMerge
      'Disconnect from the data source
      .MainDocumentType = wdNotAMergeDocument
      'Define the mailmerge type
      .MainDocumentType = wdFormLetters
      'Suppress Blank Lines
      .SuppressBlankLines = True
      'Define the output
      .Destination = wdSendToNewDocument
      'Connect to the data source, Applying the required SQL Statement,
      'including filters, on the 'SQLStatement' line and the
      ''SQLStatement1' line, for SQL Statements over 255 characters
      .OpenDataSource Name:=strWkBkNm, ReadOnly:=True, _
          AddToRecentFiles:=False, LinkToSource:=False, _
          Connection:="Provider=Microsoft.ACE.OLEDB.12.0;" & _
          "User ID=Admin;Data Source=StrWkBkNm;" & _
          "Mode=Read;Extended Properties=""HDR=YES;IMEX=1"";", _
          SQLStatement:="SELECT * FROM `Blad1$`", _
          SQLStatement1:="", SubType:=wdMergeSubTypeAccess
      With .DataSource
        .FirstRecord = wdDefaultFirstRecord
        .LastRecord = wdDefaultLastRecord
      End With
      .Execute Pause:=False
      'Disconnect from the data source
      .MainDocumentType = wdNotAMergeDocument
    End With
  End With
  .DisplayAlerts = wdAlertsAll
End With
End Sub
PS: When posting code, please use the code tags, indicated by the # button on the posting menu. Without them, your code loses much of whatever structure it had.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote