We help IT Professionals succeed at work.
Get Started

Excel Mail Merge Runtime Error (5631)

1,415 Views
Last Modified: 2013-11-25
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
Comment
Watch Question
Software testing consultant
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE