+ Reply to Thread
Results 1 to 9 of 9

From Excel macro, Open (but do not run) an existing Mail Merge Doc

Hybrid View

  1. #1
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: From Excel macro, Open (but do not run) an existing Mail Merge Doc

    I created a mail merge word document and a mail merge Excel data file on my Vista32 bit system using Excel 2003. When I open the Word document manually, I get the 'SQL' message.

    I ran an Excel macro to run the 'Mail Merge' and it was successful in that it DID NOT get the 'SQL' message. I am not confident that my solution will work on your computer. Color me skeptical. If this doesn't work for you, I still may have one or two more tricks up my sleeve, but it may take a few days.

    My files are attached (which include extras) that do the following (all independent of each other):
    a. Run the mail merge from Excel.
    b. Open a new instance of Word from Excel.
    c. Open a Word file (that has an AutoRun macro) from Excel with AutoRun enabled.
    d. Open a Word file (that has an AutoRun macro) from Excel with AutoRun disabled.

    Lewis

    Code for the mail merge follows:
    Option Explicit
    
    Private Const sWordFile = "LJMMailMerge.doc"
    Private Const sExcelMailMergeDataFile = "LJMMailMergeData.xls"
    
    
    Sub OpenMicrosoftWordMailMergeDocument()
    
      'Enumerated constants reference:
      'http://include.wutils.com/com-dll/constants/constants-Word.htm
      
      Const wdCatalog = 3
      Const wdDefaultFirstRecord = 1
      Const wdDefaultLastRecord = -16
      Const wdDirectory = 3
      Const wdDoNotSaveChanges = 0
      Const wdEMail = 4
      Const wdEnvelopes = 2
      Const wdFax = 5
      Const wdFormLetters = 0
      Const wdMailingLabels = 1
      Const wdNotAMergeDocument = -1
      Const wdOpenFormatAuto = 0
      Const wdSendToNewDocument = 0
       
    
      Dim WordApp As Object
      Dim WordDoc As Object
      
      Dim sPath As String
      Dim sPathAndWordFile As String
      Dim sPathAndExcelMailMergeDataFile As String
      
      'Assume the word file is in the same file as this file
      sPath = ActiveWorkbook.Path & "\"
      
      'Create the Word file path and file name combination
      sPathAndWordFile = sPath & sWordFile
      
      'Create the Excel Mail Merge data file path and file name combination
      sPathAndExcelMailMergeDataFile = sPath & sExcelMailMergeDataFile
      
      'Make sure the Word file exists
      If LJMFileExists(sPathAndWordFile) = False Then
        MsgBox "Micrsoft Word file DOES NOT EXIST." & vbCrLf & _
               "Folder: " & sPath & vbCrLf & _
               "File:       " & sWordFile
        Exit Sub
      End If
      
    
      'Make sure the Excel Mail Merge Data file exists
      If LJMFileExists(sPathAndExcelMailMergeDataFile) = False Then
        MsgBox "Micrsoft Word file DOES NOT EXIST." & vbCrLf & _
               "Folder: " & sPath & vbCrLf & _
               "File:       " & sExcelMailMergeDataFile
        Exit Sub
      End If
      
    
      'Create Word application object
      Set WordApp = CreateObject("Word.Application")
    
      'Create Word Document Object and Open Word file
      'Make the word application visible
      'Set the focus on the Word document
      Set WordDoc = WordApp.Documents.Open(sPathAndWordFile)
      WordApp.Visible = True
      WordApp.Application.Activate
      
      'Set the document type to 'Form Letters'
      'If some other type see the enumerated constant list above
      WordDoc.MailMerge.MainDocumentType = wdFormLetters
    
      
      'Open the mail merge data file for mail merge
      WordDoc.MailMerge.OpenDataSource _
        Name:=sPathAndExcelMailMergeDataFile, _
        AddToRecentFiles:=False, _
        Revert:=False, _
        Format:=wdOpenFormatAuto, _
        Connection:="Data Source=" & sPathAndExcelMailMergeDataFile & ";Mode=Read", _
        SQLStatement:="SELECT * FROM `Sheet1$`"
    
      'Execute the mail merge
      With WordDoc.MailMerge
        .Destination = wdSendToNewDocument
        .SuppressBlankLines = True
        With .DataSource
          .FirstRecord = wdDefaultFirstRecord
          .LastRecord = wdDefaultLastRecord
        End With
        .Execute Pause:=False
      End With
    
      'Close the original Word document that is the 'Mail Merge' Source
      WordDoc.Close SaveChanges:=wdDoNotSaveChanges
      
      'Close Microsoft Word
      'Word asks about what to do with the Mail Merge OUTPUT FILE
      WordApp.Quit
      
      'Clear object pointers
      Set WordApp = Nothing
      Set WordDoc = Nothing
    
    End Sub
    To open a new instance of Word from Excel:
    Sub OpenNewInstanceOfWordWithFocus()
    
      Application.ActivateMicrosoftApp xlMicrosoftWord
    
    End Sub
    To open a Word document (with AutoRun enabled):
    Sub OpenMicrosoftWordDocumentThatHasAutoStartMacro()
      'This opens a Microsoft Word document that has an Autorun Macro
      'to demonstrate the 'Auto Run' feature
    
      Const wdDoNotSaveChanges = 0
    
      Dim WordApp As Object
      Dim WordDoc As Object
      
      Dim sFile As String
      Dim sPath As String
      Dim sPathAndFile As String
      
      'Assume the word file is in the same file as this file
      sPath = ActiveWorkbook.Path & "\"
      
      'Create the Word file path and file name combination
      sFile = "LJMAutoStartMacro.doc"
      sPathAndFile = sPath & sFile
      
      'Make sure the Word file exists
      If LJMFileExists(sPathAndFile) = False Then
        MsgBox "Micrsoft Word file DOES NOT EXIST." & vbCrLf & _
               "Folder: " & sPath & vbCrLf & _
               "File:       " & sFile
        Exit Sub
      End If
      
      
      'Create Word application object
      Set WordApp = CreateObject("Word.Application")
    
      'Create Word Document Object and Open Word file
      'Make the word application visible
      'Set the focus on the Word document
      Set WordDoc = WordApp.Documents.Open(sPathAndFile)
      WordApp.Visible = True
      WordApp.Application.Activate
      
      'Delay so user can see progress
      'NOTE: sleep in Microsoft Word application destroys Word visibility
      Sleep 4000
      
    
      'Close Microsoft Word and discard changes
      WordDoc.Close SaveChanges:=wdDoNotSaveChanges
      WordApp.Quit
      
      'Clear object pointers
      Set WordApp = Nothing
      Set WordDoc = Nothing
    
    End Sub
    To disable AutoRun place the following line before the line that opens the Word document where 'WordApp' is the Word application object as defined in the previous example:
     'Disable AutoRun Macro
      WordApp.WordBasic.DisableAutoMacros (1)
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Open Word mail merge file from excel button
    By jtemp57 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-03-2014, 08:18 PM
  2. Replies: 2
    Last Post: 07-12-2012, 08:11 PM
  3. Missing Something - Open Word Mail Merge from Excel but no data source
    By Oz Dude in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-03-2012, 07:25 PM
  4. Open and send an existing mail via an Excel macro
    By koltregaskes in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-17-2011, 08:42 AM
  5. [SOLVED] Open a mail merge from excel: common problem
    By lightspeed in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-21-2006, 09:05 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1