Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-05-2024, 02:36 AM
catflap's Avatar
catflap catflap is offline Error 'An operation cannot be completed because of database engine errors' Windows 7 64bit Error 'An operation cannot be completed because of database engine errors' Office 2013
Advanced Beginner
Error 'An operation cannot be completed because of database engine errors'
 
Join Date: Aug 2015
Location: UK
Posts: 77
catflap is on a distinguished road
Default Error 'An operation cannot be completed because of database engine errors'

Hi


Hopefully this is posted in the right area - it's related to Access, but the essential problem is with Word:


I'm trying to automate the following on a series of Word docs from Access vba:



1. Open the word doc
2. Change data source


3. Merge to a pdf


I've got some code for changing the data source of a doc (which I got from from ChatGPT) which works fine for most circumstances:


Code:
Sub UpdateMailMergeDataSource(docPath As String, dataSourcePath As String, sqlQuery As String)
    
    Dim wdApp As Object
    Dim wdDoc As Object
    
    ' Open Word application
    On Error Resume Next
    Set wdApp = GetObject(, "Word.Application")
    If wdApp Is Nothing Then
        Set wdApp = CreateObject("Word.Application")
    End If
    On Error GoTo 0
    
    ' Open the Word document
    Set wdDoc = wdApp.Documents.Open(docPath)
    
    ' Update the data source
    With wdDoc.MailMerge
    .MainDocumentType = 0 ' wdFormLetters
    .OpenDataSource Name:=dataSourcePath, ConfirmConversions:=False, _
                    ReadOnly:=True, _
                    LinkToSource:=True, _
                    AddToRecentFiles:=False, _
                    PasswordDocument:="", _
                    PasswordTemplate:="", _
                    WritePasswordDocument:="", _
                    WritePasswordTemplate:="", _
                    Revert:=False, _
                    format:=0, Connection:="Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=" & dataSourcePath & ";Mode=Read;Extended Properties=""HDR=YES;IMEX=1"";", _
                    SQLStatement:=sqlQuery
    End With
    
    ' Save the document
    wdDoc.Save
    
    ' Close the document without prompting to save changes
    wdDoc.Close SaveChanges:=False
    
    ' Quit Word application if no other documents are open
    If wdApp.Documents.Count = 0 Then
        wdApp.Quit SaveChanges:=False
    End If
    
    ' Release objects
    Set wdDoc = Nothing
    Set wdApp = Nothing
End Sub

..however, it falls over when the doc has been linked to an excel file which has then been deleted (or has name changed). On oipening I get the error 'An operation cannot be completed because of database engine errors', which stops the code from carrying on.



The usual routine when when opening the doc manually would be to cancel the error, remove the merge source, and re-attach the new one.



So I'm looking for a way to ignore or cancel the errors which come up when you get an issue with datasource. After that the existing code should work.


Could anyone suggest a way to do this please?


Thanks!
Reply With Quote
  #2  
Old 08-06-2024, 08:30 PM
Guessed's Avatar
Guessed Guessed is offline Error 'An operation cannot be completed because of database engine errors' Windows 10 Error 'An operation cannot be completed because of database engine errors' Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,164
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Can you try this...
Application.DisplayAlerts = False

For example
Code:
Private Sub MacroName()
  Application.DisplayAlerts = False
  'Function causing the alert goes here (eg opening the document)
  Application.DisplayAlerts = True   'restore normal behaviour
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 08-07-2024, 01:11 AM
catflap's Avatar
catflap catflap is offline Error 'An operation cannot be completed because of database engine errors' Windows 7 64bit Error 'An operation cannot be completed because of database engine errors' Office 2013
Advanced Beginner
Error 'An operation cannot be completed because of database engine errors'
 
Join Date: Aug 2015
Location: UK
Posts: 77
catflap is on a distinguished road
Default

Thanks for reply.


I'm now using:


Code:
Sub test_update(docPath As String, dataSourcePath As String, sqlQuery As String)
    Dim wdApp As Object
    Dim wdDoc As Object

    ' Open Word application
    On Error Resume Next
    
   
    Set wdApp = GetObject(, "Word.Application")
    
    wdApp.DisplayAlerts = False
    
    If wdApp Is Nothing Then
        Set wdApp = CreateObject("Word.Application")
    End If
    
    
    On Error GoTo 0
    
    ' Check if Word application is open
    If wdApp Is Nothing Then
        MsgBox "Unable to open or create Word application.", vbExclamation
        Exit Sub
    End If

    ' Open the Word document
    On Error Resume Next
    'Set wdDoc = wdApp.Documents.Open(docPath)
    
    wdApp.DisplayAlerts = False
    
    Set wdDoc = wdApp.Documents.Open(docPath, False, True, False, , , , , , , , True) 'set object as Word document used as base
    
    ' Ignore specific merge file errors
    If Err.Number <> 0 Then
        Err.Clear
    End If
    On Error GoTo 0
    
    If wdDoc Is Nothing Then
        MsgBox "Unable to open the Word document: " & docPath, vbExclamation
        Exit Sub
    End If
    
    wdApp.DisplayAlerts = True

    ' Update the data source
    On Error GoTo HandleError
    With wdDoc.MailMerge
        .MainDocumentType = 0 ' wdFormLetters
        .OpenDataSource Name:=dataSourcePath, _
                        ConfirmConversions:=False, _
                        ReadOnly:=True, _
                        LinkToSource:=True, _
                        AddToRecentFiles:=False, _
                        PasswordDocument:="", _
                        PasswordTemplate:="", _
                        WritePasswordDocument:="", _
                        WritePasswordTemplate:="", _
                        Revert:=False, _
                        format:=0, _
                        Connection:="Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=" & dataSourcePath & ";Mode=Read;Extended Properties=""HDR=YES;IMEX=1"";", _
                        SQLStatement:=sqlQuery
    End With
    
    ' Check for errors during the data source update
    If Err.Number <> 0 Then
        ' Handle known database engine error by ignoring it
        Debug.Print "Error: " & Err.Number
        If Err.Number = 112 Then
            Err.Clear
        Else
            MsgBox "Error updating the data source: " & Err.Description, vbExclamation
            wdDoc.Close SaveChanges:=False
            Set wdDoc = Nothing
            Set wdApp = Nothing
            Exit Sub
        End If
    End If

    ' Save the document
    wdDoc.Save

    ' Close the document without prompting to save changes
    wdDoc.Close SaveChanges:=False

    ' Quit Word application if no other documents are open
    If wdApp.Documents.Count = 0 Then
        wdApp.Quit SaveChanges:=False
    End If

    ' Release objects
    Set wdDoc = Nothing
    Set wdApp = Nothing

    MsgBox "Mail merge data source updated and document saved.", vbInformation
    Exit Sub

HandleError:
    ' Handle errors
    If Err.Number <> 0 Then
        ' Ignore known errors related to database engine
        Debug.Print "Error: " & Err.Number
        If Err.Number = 123 Then
            Resume Next
        Else
            MsgBox "An unexpected error occurred: " & Err.Description, vbExclamation
            On Error Resume Next
            If Not wdDoc Is Nothing Then wdDoc.Close SaveChanges:=False
            If wdApp.Documents.Count = 0 Then wdApp.Quit SaveChanges:=False
            Set wdDoc = Nothing
            Set wdApp = Nothing
        End If
    End If
End Sub
As you can see I've added 'wdApp.DisplayAlerts = False' just before the line that kicks off the error messages, but it still displays them.


If it helps, below is the full sequence I have to click through to clear the errors and then be able to assign a data source (I've removed name of doc and data):


Reply With Quote
  #4  
Old 08-07-2024, 04:09 AM
Guessed's Avatar
Guessed Guessed is offline Error 'An operation cannot be completed because of database engine errors' Windows 10 Error 'An operation cannot be completed because of database engine errors' Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,164
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

I've streamlined the logic of your code so it is cleaner but I'm not sure it will solve your issue and I don't have a file to do any testing on. Give this a try and if you cross your fingers you might find it works
Code:
Sub test_update(docPath As String, dataSourcePath As String, sqlQuery As String)
    Dim wdApp As Object, wdDoc As Object

    ' Open Word application
    On Error Resume Next
      Set wdApp = GetObject(, "Word.Application")
      If wdApp Is Nothing Then
        Set wdApp = CreateObject("Word.Application")
      End If
    On Error GoTo 0
    If wdApp Is Nothing Then    ' Check if Word application is open
      MsgBox "Unable to open or create Word application.", vbExclamation
      Exit Sub
    End If

    ' Open the Word document
    On Error Resume Next
      wdApp.DisplayAlerts = False
        Set wdDoc = wdApp.Documents.Open(docPath, False, True, False, , , , , , , , True) 'set object as Word document used as base
      wdApp.DisplayAlerts = True
      ' Ignore specific merge file errors
      If Err.Number <> 0 Then Err.Clear
    On Error GoTo 0
    If wdDoc Is Nothing Then
      MsgBox "Unable to open the Word document: " & docPath, vbExclamation
      Exit Sub
    End If

    ' Update the data source
    On Error GoTo HandleError       'This will send the macro down to HandleError before returning
    With wdDoc.MailMerge
        .MainDocumentType = 0 ' wdFormLetters
        .OpenDataSource Name:=dataSourcePath, _
                        ConfirmConversions:=False, ReadOnly:=True, _
                        LinkToSource:=True, AddToRecentFiles:=False, _
                        PasswordDocument:="", PasswordTemplate:="", _
                        WritePasswordDocument:="", WritePasswordTemplate:="", _
                        Revert:=False, Format:=0, _
                        Connection:="Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=" & dataSourcePath & ";Mode=Read;Extended Properties=""HDR=YES;IMEX=1"";", _
                        SQLStatement:=sqlQuery
    End With

    ' Save and Close the document
    wdDoc.Close SaveChanges:=True

    ' Quit Word application if no other documents are open
    If wdApp.Documents.Count = 0 Then wdApp.Quit SaveChanges:=False

    ' Release objects
    Set wdDoc = Nothing
    Set wdApp = Nothing

    MsgBox "Mail merge data source updated and document saved.", vbInformation
    Exit Sub

HandleError:
    ' Handle errors
    If Err.Number <> 0 Then
        ' Ignore known errors related to database engine
        Debug.Print "Error: " & Err.Number
        If Err.Number = 123 Or Err.Number = 112 Then
          Err.Clear
          Resume Next
        Else
            MsgBox "An unexpected error occurred: " & Err.Number & vbCr & Err.Description, vbExclamation
            If Not wdDoc Is Nothing Then wdDoc.Close SaveChanges:=False
            If wdApp.Documents.Count = 0 Then wdApp.Quit SaveChanges:=False
            Set wdDoc = Nothing
            Set wdApp = Nothing
        End If
    End If
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 08-07-2024, 06:10 AM
catflap's Avatar
catflap catflap is offline Error 'An operation cannot be completed because of database engine errors' Windows 7 64bit Error 'An operation cannot be completed because of database engine errors' Office 2013
Advanced Beginner
Error 'An operation cannot be completed because of database engine errors'
 
Join Date: Aug 2015
Location: UK
Posts: 77
catflap is on a distinguished road
Default

Thanks for taking the time to do this - unfortunately it's still giving me the error messages and stopping the code.


I'm beginning to think this may not be possible - in theory one way around it would be to create a dummy data file under the name/path of the currently assigned data source before opening the document - the doc would then be able to find it when opened and not give any errors, and then I could then assign the datasource I really wanted.



The problem with that is that I don't think there's a way of getting the path to the current data source without opening the doc first, and then we're back to all the errors messages!
Reply With Quote
  #6  
Old 08-07-2024, 07:12 AM
Guessed's Avatar
Guessed Guessed is offline Error 'An operation cannot be completed because of database engine errors' Windows 10 Error 'An operation cannot be completed because of database engine errors' Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,164
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Can you explain why you are choosing to create this temp doc from a document/template that has already been created (ie one you prepared earlier). If this is the case, why would you set it up with this problem lying in wait for you?

You could instead make sure that template has no linked data source before opening it with your macro which is going to attach the datasource anyway.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #7  
Old 08-07-2024, 07:57 AM
catflap's Avatar
catflap catflap is offline Error 'An operation cannot be completed because of database engine errors' Windows 7 64bit Error 'An operation cannot be completed because of database engine errors' Office 2013
Advanced Beginner
Error 'An operation cannot be completed because of database engine errors'
 
Join Date: Aug 2015
Location: UK
Posts: 77
catflap is on a distinguished road
Default

Hi


The back story:


It's all about streamlining things. I've got 18 docs in separate folders, and my client will send over data to merge some or all of the docs every week. I've tried merging the 18 docs into one to make things easier, but there are some complications with this, so I'm using them separately.



I've already got some code set up to copy the new xlsx data files to replace the old files each time, and as the names will be the same, the merge (to a pdf file) works nicely when I call each doc from an Access function.


So far so good - the snag comes when the client wants the docs back to edit. They've started doing this every month or so lately. They previously used Indesign files but now want to switch to word docs.


I'm anticipating they will change the merge file to a different location as part of their editing process so they can see how the data merges, so when they send them back to me I will get the error I mentioned when the doc can't find the data file they set up as merge source.


If I can't find a solution to the error messages, I'll just have to open all the docs one at a time and reset the merge source manually, I just thought it would be nice to find some way to automate this and save a lot of clicking!
Reply With Quote
  #8  
Old 08-09-2024, 03:47 AM
catflap's Avatar
catflap catflap is offline Error 'An operation cannot be completed because of database engine errors' Windows 7 64bit Error 'An operation cannot be completed because of database engine errors' Office 2013
Advanced Beginner
Error 'An operation cannot be completed because of database engine errors'
 
Join Date: Aug 2015
Location: UK
Posts: 77
catflap is on a distinguished road
Default

I think I'll just have to give up on this one - looks like the windows that pop-up related to problems with mail merge just can't be controlled by automation.


Thanks Guessed for trying to help!
Reply With Quote
Reply

Tags
mail merge, vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Error 'An operation cannot be completed because of database engine errors' Office 365 conversion - Error 429 when opening Access database BruceM Word VBA 1 10-09-2018 06:22 AM
Error 'An operation cannot be completed because of database engine errors' Search Engine based on multiple critiria Macacidic Excel 14 09-09-2017 03:29 PM
MS Office 2013 error (lots of errors) Mr.Weck Office 8 01-04-2014 02:12 PM
Error 'An operation cannot be completed because of database engine errors' Associated operation ECPL_3 Excel 2 10-14-2010 05:29 PM
Error 'An operation cannot be completed because of database engine errors' Access database engine with Streets and Trips 2009 dreemsnake Misc 3 05-26-2009 10:25 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:30 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft