Solved

MAIL MERGE IN ACCESS 2007/MS WORD 2007

Posted on 2013-05-28
14
909 Views
Last Modified: 2013-06-13
Is there anyway to get ird of the pop-up error message in the mail merge process from access to word? Error "Opening this document will run the following SQL command:  Select * from [table].
0
Comment
Question by:centralmike
  • 7
  • 7
14 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39201493
You can edit the registry to suppress that security message. The details are here:
http://support.microsoft.com/kb/825765
0
 

Author Comment

by:centralmike
ID: 39201731
I am sorry we are large company.  We are not allowed to change the registry settings.  Is there anyway through code to eliminate this message from popping up?
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39201910
The only other way would be to save the main document as a 'Normal Word Document', and to re-establish it as a merge document and to reconnect the record source each time that it is opened.

This could be done in VBA code (if you allowed to use it)
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

Author Comment

by:centralmike
ID: 39202386
If possible can you send me an example of that process.  Changing the "Main Document" into a "Normal Document" through code.  When the mail merge process ends I would word to return to its normal process being the "Main Dcoument".
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39202616
Here is a sample.
Sub SetUpDocumentAndMerge(docMain As Document)

    With docMain.MailMerge
        .MainDocumentType = wdFormLetters
        .Destination = wdSendToNewDocument
        .OpenDataSource Name:="X:\MyDatabases\MyFile.mdb", _
            SQLStatement:="SELECT * FROM `MyTable`"
        .Execute
    End With
    docMain.Close wdDoNotSaveChanges 'thus saved document remains a non-merge document
    
End Sub

Open in new window

0
 

Author Comment

by:centralmike
ID: 39202663
Graham, I going to send you a copy of my module if you can add the line of code that will allow me to close the document without saving.
---------------------------------------------------------------------------------------------------------------------------------------------
Function MergeIt()

DoCmd.SetWarnings False
   Dim objWord As Word.Document
   Set objWord = GetObject("X:\Special Risk\miketesting\DEVELOPMENT\merge.docx", "Word.Document")


 
   objWord.Application.Visible = True
    objWord.MailMerge.OpenDataSource _
      Name:="X:\Special Risk\miketesting\DEVELOPMENT\SELECTIONPATHCHOOSEN.ACCDB", _
      LinkToSource:=True, _
      Connection:="TABLE TABLE1", _
      SQLStatement:="SELECT * FROM [TABLE1]"
   ' Execute the mail merge.
   
   objWord.MailMerge.Destination = wdSendToNewDocument
 ' aDoc.MailMerge.MainDocumentType = wdNotAMergeDocument

   objWord.MailMerge.Execute



objWord.Application.Options.PrintBackground = False
objWord.Application.ActiveDocument.PrintOut
'DoCmd.SetWarnings True



End Function
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39202848
You don't have a separate object variable for the Word application, so you need to keep the main document open until the printing has been done:
Function MergeIt()

DoCmd.SetWarnings False
   Dim objWord As Word.Document
   Set objWord = GetObject("X:\Special Risk\miketesting\DEVELOPMENT\merge.docx", "Word.Document")
 
   objWord.Application.Visible = True
    objWord.MailMerge.OpenDataSource _
      Name:="X:\Special Risk\miketesting\DEVELOPMENT\SELECTIONPATHCHOOSEN.ACCDB", _
      LinkToSource:=True, _
      Connection:="TABLE TABLE1", _
      SQLStatement:="SELECT * FROM [TABLE1]"
   ' Execute the mail merge.
   
   objWord.MailMerge.Destination = wdSendToNewDocument
 ' aDoc.MailMerge.MainDocumentType = wdNotAMergeDocument

   objWord.MailMerge.Execute

objWord.Application.Options.PrintBackground = False
objWord.Application.ActiveDocument.PrintOut
'DoCmd.SetWarnings True
objWord.Close wdDoNotSaveChanges

End Function  

Open in new window

0
 

Author Comment

by:centralmike
ID: 39204467
Good Morning Graham, I ran the module above and still receive the following sql error prompt.   Is there something else I am missing?
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39204967
The code doesn't actually remove merge status and link.

Open the document manually, set to a Normal Word Document and save it. Next time that it opens, it will be as such.

Our code converts it to a main document, uses it and then closes it without saving.
0
 

Author Comment

by:centralmike
ID: 39205211
Just one last question Graham.  After the merge process ends word remains open.  I would close it after the last document prints.  I tried the following statement: objWord.Application.Quit an received an error message "462"
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 39205306
I always like to use an object variable for the application as well as any documents. The code for that would look more like this
Function MergeIt()
    Dim objWordApp As Word.Application
    Dim objWord As Word.Document
    Dim bNewInstance As Boolean
    
    On Error Resume Next 'suppress error checking for next instruction only
    Set objWordApp = GetObject(, "Word.Application")
    On Error GoTo 0 'resume error checking
    
    If objWordApp Is Nothing Then
        Set objWordApp = CreateObject("Word.Application")
        bNewInstance = True
    End If
    objWordApp.Visible = True 'In case macro doesn't run to the end, leaving an invisible instance of Word.
     
    Set objWord = objWordApp.Documents.Open("X:\Special Risk\miketesting\DEVELOPMENT\merge.docx", "Word.Document")
    objWord.MailMerge.OpenDataSource _
      Name:="X:\Special Risk\miketesting\DEVELOPMENT\SELECTIONPATHCHOOSEN.ACCDB", _
      LinkToSource:=True, _
      Connection:="TABLE TABLE1", _
      SQLStatement:="SELECT * FROM [TABLE1]"
    
    objWord.MailMerge.Destination = wdSendToNewDocument
    
    ' Execute the mail merge.
    objWord.MailMerge.Execute
    
    objWordApp.Options.PrintBackground = False
    objWordApp.ActiveDocument.PrintOut
    
    'close the documents
    objWordApp.ActiveDocument.Close wdDoNotSaveChanges
    objWord.Close wdDoNotSaveChanges
    
    'Close application if it was created for this merge
    If bNewInstance Then
        objWordApp.Quit
    End If
End Function

Open in new window

0
 

Author Comment

by:centralmike
ID: 39205362
Now I am receiving a runtime error 13 on the following line.

  Set objWord = objWordApp.Documents.Open("X:\Special Risk\miketesting\DEVELOPMENT\merge.docx", "Word.Document")
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39205395
Oops. I couldn't actually test the code because it referred to your folders. I changed your GetObject line into the Open code and failed to remove the ' , "Word.Document"' bit.

it should read:
 Set objWord = objWordApp.Documents.Open("X:\Special Risk\miketesting\DEVELOPMENT\merge.docx")
0
 

Author Closing Comment

by:centralmike
ID: 39246081
Thanks for all the help Graham.  Sorry for the delay I had to leave town.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Outlook Free & Paid Tools
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question