Link to home
Start Free TrialLog in
Avatar of tokerblue
tokerblue

asked on

Excel Mail Merge Runtime Error (5631)

I recently inheirited a Excel spreadsheet that I am having a lot of difficulty with.  I'm getting a 5631 runtime error when clicking on a mail merge button.  I believe it's opening a .dot file and creating a new document and then merging it with an Access database. The code looks too convoluted for me to fix or correct.  Is there an easy way to implement the "save" fix listed on the Microsoft link?

5631 Link:
http://support.microsoft.com/kb/828388

Code in Excel Document:
Dim MyWord As Word.Application, myDoc As Word.Document
Dim wrkJet As Workspace
    Dim dbsBackend As Database
    Dim rstRep As Recordset
    Dim i As Integer
   
 
    Application.ScreenUpdating = False
    'Create Microsoft Jet Workspace object.
   
    Set wrkJet = CreateWorkspace("", "admin", "", dbUseJet)

    ' Open Database object from saved Microsoft Jet database
    Set dbsBackend = wrkJet.OpenDatabase(Range("RepositoryLocation").Value, False)
    Set rstRep = dbsBackend.OpenRecordset("qryFeePagesMailMerge")
     rstRep.MoveLast
     rstRep.MoveFirst
     
For i = 1 To rstRep.RecordCount
   If rstRep![tblRepository.RepositoryAutoID] = Range("pricingid").Value And rstRep!ProductPlatform = "MOM" Then
   MsgBox ("Select Retail.")
   Exit Sub
   ElseIf rstRep![tblRepository.RepositoryAutoID] = Range("pricingid").Value Then
   Exit For
   End If
  rstRep.MoveNext
    Next
     
    Set MyWord = CreateObject("Word.Application")
    MyWord.Application.ScreenUpdating = False
    MyWord.Visible = True
    Set myDoc = MyWord.Documents.Add(Range("FeePageTemplateLocation").Value & Range("FeePageTemplateClientRetail").Value)

    With myDoc.MailMerge
        .OpenDataSource Name:=Range("RepositoryLocation").Value, _
        ConfirmConversions:=False, ReadOnly:=True, LinkToSource:=True, _
        AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
        WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
        Format:=wdOpenFormatAuto, Connection:="QUERY qryFeePagesMailMerge" _
        , SQLStatement:="SELECT * FROM `qryFeePagesMailMerge`", SQLStatement1:=""
        .Destination = wdSendToNewDocument
        .SuppressBlankLines = True


   
        With .DataSource
            .FirstRecord = i
            .LastRecord = i
        End With
        .Execute Pause:=False
    End With
    'MyWord.Documents(Range("FeePageTemplate").Value).Close
    myDoc.Close SaveChanges:=False
    MyWord.Application.ScreenUpdating = True
    MyWord.ActiveDocument.PrintPreview
    Application.ScreenUpdating = True
ASKER CERTIFIED SOLUTION
Avatar of Jeroen Rosink
Jeroen Rosink
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial