Results 1 to 7 of 7
  1. #1
    alpinegroove is offline Competent Performer
    Windows XP Access 2002
    Join Date
    Jan 2011
    Posts
    114

    Access to Word Merge into Separately Saved Documents

    I use the following code to merge data from Access into a Word document in which the merge had been set up.
    The result is a long list of letters in one Word file.

    Is there a way to add code that would save each letter separately?
    The file name should be based on two fields from the same query the merge is based on: qryComp.


    The fields are [LastName] and [CourseTitle].

    Thank you.

    Code:
    Private Sub Command1_Click()
    
         
            Dim strPath As String
            Dim strDataSource As String
            Dim conTemplate As String
            Dim conQuery As String
         
            Dim doc As Word.Document
            Dim wrdApp As Word.Application
         
            conTemplate = "11 Instructor Contract Template.doc"
            conQuery = "qryComp"
         
            On Error GoTo HandleErrors
            ' Delete the rtf file, if it already exists.
            strPath = "H:\"
            strDataSource = strPath & conQuery & ".doc"
            Kill strDataSource
         
            ' Export the data to rtf format.
            DoCmd.OutputTo acOutputQuery, conQuery, _
             acFormatRTF, strDataSource, False
         
            ' Start Word using the mail merge template.
            Set wrdApp = New Word.Application
            Set doc = wrdApp.Documents.Add(strPath & conTemplate)
         
            ' Do the mail merge to a new document.
            With doc.MailMerge
                .OpenDataSource Name:=strDataSource
                .Destination = wdSendToNewDocument
                .SuppressBlankLines = True
                With .DataSource
                    .FirstRecord = wdDefaultFirstRecord
                    .LastRecord = wdDefaultLastRecord
                End With
                If .State = wdMainAndDataSource Then
                    .Execute
                End If
            End With
         
            ' Display the mail merge document.
            wrdApp.Visible = True
         
         
    ExitHere:
            Set doc = Nothing
            Set wrdApp = Nothing
            Set doc2 = Nothing
            Set wrdApp2 = Nothing
            Exit Sub
         
    HandleErrors:
            Select Case Err.Number
                Case 53         ' File not found.
                    Resume Next
                Case Else
                    MsgBox Err.Number & ": " & Err.Description
                    Resume ExitHere
            End Select
         
         
         
        End Sub

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Google: access word mail merge save letters separate files

    Here is one

    http://www.gmayor.com/individual_merge_letters.htm
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    alpinegroove is offline Competent Performer
    Windows XP Access 2002
    Join Date
    Jan 2011
    Posts
    114
    Thanks for the link. I saw it earlier. Isn't that code for Word, not Access?

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Didn't notice but guess it is set up behind Word document. However, it is VBA and should be adaptable to run behind Access although might be better and easier behind the Word document. Do you have a saved Word document that is used for this merge? I suppose code could go there. I have never coded for Word.

    Else, try Google some more.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    alpinegroove is offline Competent Performer
    Windows XP Access 2002
    Join Date
    Jan 2011
    Posts
    114
    Ok, thanks.

  6. #6
    alpinegroove is offline Competent Performer
    Windows XP Access 2002
    Join Date
    Jan 2011
    Posts
    114
    Update: I ended up using the following VBA code in Word. It is based on the widely discussed add-on:
    http://www.gmayor.com/individual_merge_letters.htm

    My document has a header/letterhead, so I could not use the Normal template. I had to create a different template and direct the code to it.

    So my Access VBA code merges the document and the opens the Word macro. I would rather have all of this happen via Access, but this will do for now.

    Thanks for the help.

    Sub SplitMergedLetter()

    ' splitter Macro modified to save individual letters with
    ' information from data source. The filename data must be added to
    ' the top of the merge letter - see web article.

    Dim sName As String
    Dim docName As String
    Dim Letters As String
    Dim Counter As Long
    Dim oDoc As Document
    Dim oNewDoc As Document

    Set oDoc = ActiveDocument
    oDoc.SaveAs "C:\Documents and Settings\xyz\Desktop\MergeTest\MergedContracts.doc "
    Selection.EndKey Unit:=wdStory
    Letters = Selection.Information(wdActiveEndSectionNumber)
    Selection.HomeKey Unit:=wdStory
    Counter = 1
    While Counter < Letters
    Application.ScreenUpdating = False
    With Selection

    .HomeKey Unit:=wdStory
    .EndKey Unit:=wdLine, Extend:=wdExtend
    .MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend

    End With

    sName = Selection
    docName = "C:\Documents and Settings\xyz\Desktop\MergeTest\" & sName & ".doc"

    oDoc.Sections.First.Range.Cut
    Set oNewDoc = Documents.Add(Template:="C:\Documents and Settings\xyz\Application Data\Microsoft\Templates\Contract Template.dot")

    With Selection
    .Paste
    .HomeKey Unit:=wdStory
    .MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
    .Delete
    End With
    oNewDoc.SaveAs filename:=docName, _
    FileFormat:=wdFormatDocument, _
    AddToRecentFiles:=False
    ActiveWindow.Close
    Counter = Counter + 1
    Application.ScreenUpdating = True
    Wend

    oDoc.Close wdDoNotSaveChanges
    End Sub

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Congratulations! Glad you got a solution.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 11-30-2011, 07:06 PM
  2. Access to merge documents
    By SJames in forum Access
    Replies: 2
    Last Post: 04-25-2011, 09:27 AM
  3. Mail merge from from Access to Word
    By williamgladstone in forum Access
    Replies: 1
    Last Post: 03-22-2011, 12:00 PM
  4. Categorizing Word documents with Access
    By radagast in forum Access
    Replies: 0
    Last Post: 06-14-2009, 12:06 AM
  5. Mail Merge from Access to Word
    By Rachelkm2 in forum Programming
    Replies: 1
    Last Post: 05-29-2009, 02:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums