View Single Post
 
Old 03-31-2016, 07:32 PM
macropod's Avatar
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,375
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

The code looks alright but, if the document has been saved as a mailmerge main document, your code will stop, waiting for the SQL prompt to be responded to. You can circumvent that with:
Code:
With objWord
    .DisplayAlerts = False
    Set oDoc = objWord.Documents.Open(FileName:=fName, ConfirmConversions:=True, _
        ReadOnly:=True, AddToRecentFiles:=False, Visible:=False)
    StrSrc = "H:\PWS\Parks\Parks Operations\Sports\Sports15\DATA1\" & ws_vh.Range("B4")
    With oDoc
        With .MailMerge
            .MainDocumentType = wdDirectory
            .Destination = wdSendtToNewDocument
            .SuppressBlankLines = True
            .OpenDataSource _
              Name:=StrSrc, ReadOnly:=True, AddToRecentFiles:=False, LinkToSource:=False, _
              Connection:="Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;" & _
                "Data Source=StrSrc;Mode=Read;Extended Properties=""HDR=YES;IMEX=1"";", _
                SQLStatement:="SELECT * FROM `CORE$` WHERE [TYPE]='" & itype & "' AND [SIG_CREW]= '" & isubresp & "' ORDER BY [Start] ASC, [COMPLEX] ASC, [UNIT] ASC", _
                SQLStatement1:="", SubType:=wdMergeSubTypeAccess
            .Execute Pause:=False
        End With
        .Close False
    End With
    .DisplayAlerts = True
End With
It might also be worthwhile checking what's being passed via fName and that that file actually contains the relevant mergefields.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote