View Single Post
 
Old 08-23-2014, 01:50 AM
tariqaleed2 tariqaleed2 is offline Windows 7 32bit Office 2010 32bit
Novice
 
Join Date: Jul 2014
Posts: 14
tariqaleed2 is on a distinguished road
Default

Thank you very much macropod YOU help me a lot


The best way I found , is forcing the user to closing ALL WORD Documents before making action .
If there is another way, TO NOT be THE user forcing the closing ALL WORD Documents TO BE better else the problem was solved .




I repeat my thanks to you and TO the members
Greetings to all
Code:
Private Sub COMM1_Click() 
    Dim objWord As Word.Application 
    Dim doc, Target As Word.Document 
    Dim sDBPath As String 
    On Error Resume Next 
    Set objWord = GetObject(, "Word.Application") 
    On Error Goto 0 
    If Not objWord Is Nothing Then 
        MsgBox "one or more Word file is open ... Please closing the all Word documents before executing this action" 
        Exit Sub 
    End If 
    Set objWord = CreateObject("Word.Application") 
    objWord.Visible = False 'True
    Set doc = objWord.Documents.Open(Application.CurrentProject. Path & "\WORD" & ".DOCX") 
    With doc.MailMerge 
        .MainDocumentType = wdFormLetters 
        sDBPath = Application.CurrentProject.Path & "\DATA1" & ".XLS" 
        .OpenDataSource Name:=sDBPath, _ 
        SQLStatement:="SELECT * FROM `wordqur1$`" 
    End With 
    With doc 
        .MailMerge.Destination = wdSendToNewDocument 
        .MailMerge.Execute Pause:=False 
        With objWord.ActiveDocument 
            .SaveAs FileName:="C:\Users\TARIQ-LABTOP2\Desktop\rrrrrrrrrrrr.docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False 
             ' and/or:
            .Close SaveChanges:=False 
        End With 
    End With 
    doc.Saved = True 
    objWord.Quit 
    Set doc = Nothing 
    Set objWord = Nothing 
    MsgBox "Successfully complete a mail merge" 
End Sub

Last edited by macropod; 08-23-2014 at 03:04 AM. Reason: Added code tags & formatting
Reply With Quote