Solved

MAIL MERGE IN ACCESS 2007/MS WORD 2007

Posted on 2013-05-28
14
873 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
 

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Loop within Select Case 3 26
Resize text 4 16
Multiple queries for a form 14 22
Pass Form Control to VBA Function 2 27
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now