Merge with MS Word 2002 
Author Message
 Merge with MS Word 2002

Hello All

I wonder if you could help me with a mail merge problem?
The code below has a small bug in it, when i click the mail merge button it
only picks up the first record and not the current record being viewed.

I wrote a GUI for the database in VB (using ado) and if i'm clicking through
the database it dosent matter if i'm on the first record of the last record
it always merges the first record only.

I wanted to use DDE to copy each text in each textbox to word but thought
that a bad idea

Thanks for your help.

John

Private Sub cmdMailMerge_Click()
Set oApp = CreateObject("Word.Application")

Dim oMainDoc As Word.Document
    Dim oSel As Word.Selection
    Dim sDBPath As String

    'Start a new main document for the mail merge.
    Set oMainDoc = oApp.Documents.Add

    With oMainDoc.MailMerge

        .MainDocumentType = wdFormLetters

        'Set up the mail merge data source to your database
        sDBPath = "C:\Address\Contacts.mdb"
        .OpenDataSource Name:=sDBPath, _
           SQLStatement:="SELECT * FROM [Contacts]"

                   .DataSource.ActiveRecord = wdLastDataSourceRecord

        'Add the field codes to the document to create the form letter.
        With .Fields
            Set oSel = oApp.Selection
            .Add oSel.Range, "FirstName"
            oSel.TypeParagraph
            .Add oSel.Range, "LastName"
            oSel.TypeParagraph
            .Add oSel.Range, "Address1"
            oSel.TypeParagraph
            .Add oSel.Range, "Address2"
            oSel.TypeParagraph
            .Add oSel.Range, "Address3"
             oSel.TypeParagraph
            .Add oSel.Range, "Town"
             oSel.TypeParagraph
            .Add oSel.Range, "County"
             oSel.TypeParagraph
            .Add oSel.Range, "Postalcode"

'            oSel.TypeParagraph
'            oSel.TypeText "Dear "
'            .Add oSel.Range, "ContactName"
'            oSel.TypeText ","
'            oSel.TypeParagraph
'            oSel.TypeParagraph
'            oSel.TypeText " This letter is to inform you..."
'            oSel.TypeParagraph
'            oSel.TypeParagraph
'            oSel.TypeText "Sincerely, [Your Name Here]"
        End With
    End With

    'Perform the mail merge to a new document.
    With oMainDoc
        .MailMerge.Destination = wdSendToNewDocument
        .MailMerge.Execute Pause:=False
    End With



Fri, 03 Sep 2004 02:20:50 GMT  
 Merge with MS Word 2002
It looks like your mail merge code just opens a record source and uses the
first record it sees.

The record your VB app is currently viewing will have a primary key field or
some other thing that uniquely identifies that record.  Add a couple lines
of code in your mail merge routine that use something like RS.FindFirst on
the PK of the currently viewed record.

--

Peace & happy computing,

Mike Labosh, MCSD MCT
Owner, vbSensei.Com
"Debugging LISP code is like reading a chatroom full of emoticons on
crack." -- vbSensei



Fri, 03 Sep 2004 02:42:02 GMT  
 Merge with MS Word 2002
Thanks for that Mike but can you elaborate a bit?


Quote:
> It looks like your mail merge code just opens a record source and uses the
> first record it sees.

> The record your VB app is currently viewing will have a primary key field
or
> some other thing that uniquely identifies that record.  Add a couple lines
> of code in your mail merge routine that use something like RS.FindFirst on
> the PK of the currently viewed record.

> --

> Peace & happy computing,

> Mike Labosh, MCSD MCT
> Owner, vbSensei.Com
> "Debugging LISP code is like reading a chatroom full of emoticons on
> crack." -- vbSensei



Fri, 03 Sep 2004 03:17:25 GMT  
 Merge with MS Word 2002
Let's say I have to print a form letter to my customers.  This means the
records in my Customer table.  My VB form contains a way that I can browse
through the records or somehow select one or more of the records.  I would
want to make sure that one of the fields on the form is the Customer ID (or
whatever other field the database refers to as the "Primary Key".

Then in your code that does the mail merge, you either use a WHERE clause in
your SQL that gets the records so that you only get the ones you want to
mail merge, or you will need to have some code that navigates through the
recordset so that it is pointing at the appropriate record before doing the
merge.

--

Peace & happy computing,

Mike Labosh, MCSD MCT
Owner, vbSensei.Com
"Debugging LISP code is like reading a chatroom full of emoticons on
crack." -- vbSensei



Fri, 03 Sep 2004 03:31:55 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Merge with MS Word 2002

2. Merge with MS Word 2002

3. spped problems with MS Access 2002 as client, Word 2002 as server

4. Word 2002: How to save a file in Word 97 format in Word 2002

5. word 2002 - mail merge

6. MS Word 2002 Font Default

7. MS Word 2002 quick question?

8. mail-merge MS Word and MS Access via ASP

9. Access 2002 Word 2002 Automation

10. MailEnvelope property in Excel 2002 and Word 2002

11. Word 2002 Access 2002 Automation

12. Word Mail Merge Err - Word could not merge...

 

 
Powered by phpBB® Forum Software