I have inherited a very involved access database which requires the merging of queries to existing Word documents. There are several forms which have the VBA calling the dmerge command. Each form defines the specific query and specific word document to merge. The General Module to do this is called "dmerge". This seems to have stopped working. Simply stated, dmerge calls up a string query, then mail merges it to an existing string word document template, then closes the query. I keep receiving a '91 error on the line "If rst.EOF = True Then" . The general module code which gives the error is below. Creating reports is not an option because this database has over 25 Word Templates it used to merge to in Access 2013 depending on which form calls up the dmerge. Thank you if anyone is able to suggest a VBA repair to get the run query, send to word document general module below working. I am an intermediate user.
Code:Public Function dmerge(StrTemplate As String, StrQuery As String, strFolder As String)DoCmd.Hourglass True 'pubCurrDBPath is a variable located in the GenMods module 'it is normally primed when db is opened via macro 'AutoExec(Runcode OpenProcs) by a call to genCurrDBPath Dim dbs As DAO.Database Dim rst As DAO.Recordset Dim qdf As DAO.QueryDef 'pubTemplateFolder is a constant located in the GenMods module StrTemplate = pubTemplateFolder & StrTemplate If StrQuery = "none" Then GoTo SkipQuery 'Run query 'exit function if recordset is empty If rst.EOF = True Then MsgBox "No data to merge.", vbInformation, "Mail Merge" rst.Close GoTo Exit_Here End If rst.Close qdf.Close SkipQuery: Dim wApp As Word.Application Dim wDoc As Word.Document Set wApp = New Word.Application Set rst = CurrentDb.OpenRecordset("StrQuery") On Error GoTo dMergeError ' open word merge template document Set wDoc = wApp.Documents.Open(StrTemplate) 'Dim strSampleFile 'strSampleFile = pubCurrDBPath & "Sample.doc" 'if there is no query associated with the passed strTemplate 'then goto procedure exit If StrQuery <> "none" Then GoTo DodMerge 'StrQuery "none" means that the document is a blank 'form with no merge required 'this copies the template document and pastes it to a 'new open document wDoc.Select wApp.Selection.Copy 'close the templates document wDoc.Close (wdDoNotSaveChanges) 'Documents.Add DocumentType:=wdNewBlankDocument wApp.Documents.Add DocumentType:=wdNewBlankDocument wApp.Selection.Paste GoTo FinishUpDoc 'merge processing DodMerge: On Error GoTo dMergeError With wDoc.MailMerge .MainDocumentType = wdFormLetters .SuppressBlankLines = True .Destination = wdSendToNewDocument .OpenDataSource Name:=StrQuery, Format:=wdOpenFormatDocument, LinkToSource:=False, Connection:="strQuery", subtype:=wdMergeSubTypeWord .Execute End With 'close the merge 'template document' without saving wDoc.Close (wdDoNotSaveChanges) GoTo FinishdMerge End With On Error GoTo dMergeError 'paste the contents of the clipboard over the current selected text wApp.Selection.Paste '2/25/05 ME: add the following statement-see if it will give another case On Error GoTo dMergeError dMergeError: DoCmd.Hourglass False Select Case Err.Number Case 3265 MsgBox ("Query named " & StrQuery & " cannot be located.") Case 5174 MsgBox ("Word doc named " & StrTemplate & " cannot be located.") ActiveDocument.Close Case 5922 MsgBox ("Data transferred to Word cannot contain quotes!") ActiveDocument.Close SaveChanges:=wdPromptToSaveChanges, OriginalFormat:=wdPromptUser Case Else MsgBox ("error # " & Err.Number & Err.Description) End Select Resume Exit_Here End Function