macro to mail merge and print

davidjbroadway
davidjbroadway used Ask the Experts™
on
how do I create a macro to mail merge with word and print.
so that i can link this to a button in access.
Funke
this is what I tried from your responce I am getting an error @ CHECK IF MSWord IS OPEN
also is "Gort" req in my file ref?

sorry I'm new to this

Private Sub Command166_Click()
Dim sWordDocumentToOpenString
aWordDocumentToOpen = "\\Gort\C\My Documents\temp\A1000 MOD4 R&O print.doc"

Dim strSQL As String
Dim appWord As Object
Dim blnAlreadyRunning As Boolean

On Error Resume Next

CHECK IF MS Word IS OPEN
blnOpen = True
Set appWord = GetObject(, "word.application")

If Err.Number <> 0 Then
   blnOpen = False
   Err.Clear
End If
If Not blnOpen Then
  Set appWord = CreateObject("word.application")
End If

If Not appWord.Visible Then
  appWord.Visible = True
End If

'Open the file in Word
appWord.Documents.Open sWordDocumentToOpen
   
'Adjust the active document's data source
With appWord.ActiveDocument.MailMerge.DataSource

   strSQL = "SELECT * " & _
       "FROM SOMETABLE"

   .MainDocumentType = wdFormLetters
   .OpenDataSource Name:="C:\NorthWind.mdb", _
            SQLStatement:="SELECT * FROM SOMETABLE"
   
End With

'MERGE
appWord.ActiveDocument.MailMerge.Execute

'CLEAN UP
Set appWord = Nothing







End Sub
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
dim conWordDocumentToOpen as string
conWordDocumentToOpen = "\\Gort\C\FormLetters\FormLetter.doc"

Dim strSQL As String            
Dim appWord As Object            
Dim blnAlreadyRunning as Boolean

On Error Resume Next

CHECK IF MS Word IS OPEN
blnOpen = True
Set appWord = GetObject(, "word.application")
If Err.Number <> 0 Then
    blnOpen = False
    Err.Clear
End If
If Not blnOpen Then
Set appWord = CreateObject("word.application")
If Not appWord.Visible Then appWord.Visible = True

'Open the file in Word
appWord.Documents.Open conWordDocumentToOpen
   
'Adjust the active document's data source
With appWord.ActiveDocument.MailMerge.DataSource

    strSQL = "SELECT * " & _
        "FROM SOMETABLE"

    .MainDocumentType = wdFormLetters
    .OpenDataSource Name:="C:\NorthWind.mdb", _
             SQLStatement:="SELECT * FROM SOMETABLE"
   
End With

'Merge the document
appWord.ActiveDocument.MailMerge.Execute

'Remove reference to MS Word
Set appWord = Nothing

Commented:
dim conWordDocumentToOpen as string
conWordDocumentToOpen = "\\Gort\C\FormLetters\FormLetter.doc"

Dim strSQL As String            
Dim appWord As Object            
Dim blnAlreadyRunning as Boolean

On Error Resume Next

CHECK IF MS Word IS OPEN
blnOpen = True
Set appWord = GetObject(, "word.application")
If Err.Number <> 0 Then
    blnOpen = False
    Err.Clear
End If
If Not blnOpen Then
Set appWord = CreateObject("word.application")
If Not appWord.Visible Then appWord.Visible = True

'Open the file in Word
appWord.Documents.Open conWordDocumentToOpen
   
'Adjust the active document's data source
With appWord.ActiveDocument.MailMerge.DataSource

    strSQL = "SELECT * " & _
        "FROM SOMETABLE"

    .MainDocumentType = wdFormLetters
    .OpenDataSource Name:="C:\NorthWind.mdb", _
             SQLStatement:="SELECT * FROM SOMETABLE"
   
End With

'Merge the document
appWord.ActiveDocument.MailMerge.Execute

'Remove reference to MS Word
Set appWord = Nothing

Commented:
dim conWordDocumentToOpen as string
conWordDocumentToOpen = "\\Gort\C\FormLetters\FormLetter.doc"

Dim strSQL As String            
Dim appWord As Object            
Dim blnAlreadyRunning as Boolean

On Error Resume Next

CHECK IF MS Word IS OPEN
blnOpen = True
Set appWord = GetObject(, "word.application")
If Err.Number <> 0 Then
    blnOpen = False
    Err.Clear
End If
If Not blnOpen Then
Set appWord = CreateObject("word.application")
If Not appWord.Visible Then appWord.Visible = True

'Open the file in Word
appWord.Documents.Open conWordDocumentToOpen
   
'Adjust the active document's data source
With appWord.ActiveDocument.MailMerge.DataSource

    strSQL = "SELECT * " & _
        "FROM SOMETABLE"

    .MainDocumentType = wdFormLetters
    .OpenDataSource Name:="C:\NorthWind.mdb", _
             SQLStatement:="SELECT * FROM SOMETABLE"
   
End With

'Merge the document
appWord.ActiveDocument.MailMerge.Execute

'Remove reference to MS Word
Set appWord = Nothing
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Commented:
dim sWordDocumentToOpen as string
sWordDocumentToOpen = "\\Gort\C\FormLetters\FormLetter.doc"

Dim strSQL As String            
Dim appWord As Object            
Dim blnAlreadyRunning as Boolean

On Error Resume Next

CHECK IF MS Word IS OPEN
blnOpen = True
Set appWord = GetObject(, "word.application")

If Err.Number <> 0 Then
    blnOpen = False
    Err.Clear
End If
If Not blnOpen Then
   Set appWord = CreateObject("word.application")
End if

If Not appWord.Visible Then
   appWord.Visible = True
End if

'Open the file in Word
appWord.Documents.Open sWordDocumentToOpen
   
'Adjust the active document's data source
With appWord.ActiveDocument.MailMerge.DataSource

    strSQL = "SELECT * " & _
        "FROM SOMETABLE"

    .MainDocumentType = wdFormLetters
    .OpenDataSource Name:="C:\NorthWind.mdb", _
             SQLStatement:="SELECT * FROM SOMETABLE"
   
End With

'MERGE
appWord.ActiveDocument.MailMerge.Execute

'CLEAN UP
Set appWord = Nothing

Commented:
I swear I did not post this 4 times...

Commented:
USE THE LAST POST - I THINK THE OTHERS WERE PREMATURE

Commented:

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
 - Answered by: funke
Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

Nic;o)

Author

Commented:
I havn't got it all worked out but thanks for the response

Commented:
Gort - no, just put the path to your file - that was the name of someones computer where the file was.  Just put the path to your file in place of that string.

the error is because that is supposed to be a comment - just put a single quote in front of that line or delete it.

Author

Commented:
Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial