473,402 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Automate the closing of Word from Access after MailMerge

I'm trying to adapt and use code provided by 'Leigh P' to automate a mailmerge from Access, but it is not working.

I'll paste my code below. I have the code saved in a module, and I'm calling the function from a command button on a form.

The error I'm currently getting is a Microsoft Office Word error: "The Microsoft Office Access database engine cannot find the input table or query 'ailmerge_Source_Safeguardin'. Make sure it exists and that its name is spelled correctly."

The query definitely exists, and I know I've spelled it correctly as I copied and pasted the name into the code. Why when it executes is it omitting the first and last characters of the query name?

Any help much appreciated. Thank you.



Expand|Select|Wrap|Line Numbers
  1. Function MergeIt()
  2.  
  3. On Error GoTo ErrHandling
  4.  
  5. Dim objDoc As Word.Document
  6. Dim objWord As Word.Application
  7. Dim blnCreated As Boolean
  8. Dim strFilename As String
  9. Dim strQueryName As String
  10. Dim strDBpath As String
  11.  
  12. strFilename = "C:\Users\Laura\Documents\Safeguarding_Certificate.docx"
  13. strQueryName = "Mailmerge_Source_Safeguarding"
  14. strDBpath = "C:\Users\Laura\Documents\Absences.accdb" 'Or probably just CurrentDb.Name
  15.  
  16. On Error Resume Next
  17.  
  18. Set objWord = GetObject(, "Word.Application")
  19.  
  20. If Err Then
  21. Set objWord = CreateObject("Word.Application")
  22. blnCreated = True
  23. End If
  24.  
  25. On Error GoTo ErrHandling
  26.  
  27. Set objDoc = objWord.Documents.Open(strFilename)
  28.  
  29. 'Make Word Visible
  30. objWord.Visible = True
  31.  
  32. 'Execute the MailMerge
  33. With objDoc.MailMerge
  34. 'Set Merge Data Source
  35. objDoc.MailMerge.OpenDataSource Name:=strDBpath, _
  36. LinktoSource:=True, _
  37. Connection:="QUERY " & strQueryName, _
  38. SQLStatement:="SELECT * FROM " & strQueryName
  39. .Destination = wdSendToNewDocument
  40. .Execute
  41. objWord.ActiveDocument.PrintOut False
  42. objWord.ActiveDocument.Close wdDoNotSaveChanges
  43. End With
  44.  
  45. 'Close The form files and the merged document
  46. objDoc.Close wdDoNotSaveChanges
  47.  
  48. If blnCreated Then
  49. objWord.Quit
  50. End If
  51.  
  52. Set objDoc = Nothing
  53. Set objWord = Nothing
  54.  
  55. Exit Function
  56.  
  57. ErrHandling:
  58. MsgBox "Whoops" 'Better error handling of course
  59.  
  60. End Function
Aug 14 '14 #1
3 1252
jimatqsi
1,271 Expert 1GB
Laura,
Why does the description of your error differ from the title of the post?

That's very odd about leaving out the first and last characters. Just to start somewhere, have you tried modifying the name in the code, like adding a space to the beginning and end, just to see what happens?

Jim
Aug 14 '14 #2
That was the title of the post from 'Leigh P's discussion, but it also describes what I'm hoping to achieve.

When I change the name of the query to include underscores at the start and end of the name, it then appears to skip past that error and then the next issue I get is an ODBC Microsoft Access Driver Login Failed error message: Could not find the file 'C:\Users\Laura\Documents.mdb'. I can't see why it's trying to find that file - I haven't declared it anywhere.

Is there a wider issue causing all these problems? I have added Object 12 References to Word and Access - are there any other references I should add?

Thanks for your help.
Aug 14 '14 #3
NeoPa
32,556 Expert Mod 16PB
Laura,
It would be very helpful (now the code is posted with visible line numbers) if you could indicate exactly which line the error occurs on.

Let's start with the first error first. Get that understood and resolved and then think about moving on to the second.

Please start by checking that your code compiles correctly (See Before Posting (VBA or SQL) Code).
Aug 14 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: cyranoVR | last post by:
This is the approach I used to automate printing of Microsoft Access reports to PDF format i.e. unattended and without annoying "Save As..." dialogs, and - more importantly - without having to use...
1
by: Jim | last post by:
I've got a routine that creates a Mail merge doc in Word. I'd like to add a title and/or header to the document. How can I do this in code? In other words, either before or after I execute the...
4
by: selen | last post by:
Hello, My problem is that: I want to open a word document and writing something in it then save it.But it give me granting access error.and say that:To grant ASP.NET write access to a file,...
2
by: Paul | last post by:
I've written a large C# app to instantiate Word 2003. Several tables are created as well as a Merge Table containing data that is merged in throughout the document. I'd like to toss in some...
0
by: Scott May | last post by:
I am doing a mailmerge from vb.net to word 2003. Everything works fine, but I add a fill-in field to the wrod document and then do a mailmerge the toolbars are missing. Can anyone give me a clue? ...
0
by: Giovanni pepe | last post by:
In my application .NET I must Intercept closing word What better method?
0
by: Victor Angelier | last post by:
He can anyone help me connecting a DSN datasource to a Word document with Word 2003 ? I am stuck here: Object oConnection = "ODBC; DSN=ez_planner"; Object oQuery = strQuery; Object oSubType...
7
by: nadsweb | last post by:
Hi I have a very strange Problem. I have tried to import data from an access query into word per mailmerge. This usually works well, however with this new database i can only choose from a list of...
0
by: Beta | last post by:
Does anyone know how to close an embedded word document in C# application without closing an external Word document? The issue is the external Word Document share the same instance (winword.exe)...
4
by: peter1952 | last post by:
I would like to automate closing an Access 2003 database at the end of running a macro. How can I do this?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.