Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mail merging in VB6

Status
Not open for further replies.

Denster

Programmer
Apr 9, 2001
198
GB
I have created a project using vb6 as a front end and access 2000 as the database. I now want to add a mail merge to the project, so that a template letter can be sent out to the relevant adresses. I have played around with the rich text box to achieve this but not getting anywhere. Can anybody point me in the right direction.
 
If you already have the word document set up with the correct SQL then you can just open the file and print it. You will also need a reference to the Microsoft Word Object.
Code:
Dim wrd As Word.Application
Dim doc As Word.Document
    
Set wrd = CreateObject("Word.Application")
wrd.Visible = False
Set doc = wrd.Documents.Open("C:\My Documents\Test.doc")
doc.PrintOut True
Do While wrd.BackgroundPrintingStatus <> 0
Loop
doc.Close
wrd.Quit
Set doc = Nothing
Set wrd = Nothing
If you need to change the SQL based on criteria then you need to check out the options in doc.MailMerge
 
What about using the rich text box as Denster asked instead of using Word?
 
If I did use word, how could I change the SQL within my application at run time?. I would prefer rich text though.
 
If you want to change the SQL using the word document just set up the document to look at the database but change the datasource. i.e.
Code:
    Dim wrd As Word.Application
    Dim doc As Word.Document
    
    Set wrd = CreateObject(&quot;Word.Application&quot;)
    wrd.Visible = True
    Set doc = wrd.Documents.Open(&quot;c:\mendal\address.doc&quot;)
    ActiveDocument.MailMerge.DataSource.QueryString = _
    &quot;SELECT * FROM Table1 WHERE FIELD = '&quot; & MyVariable & &quot;'&quot;
    doc.PrintOut True
    Do While wrd.BackgroundPrintingStatus <> 0
    Loop
    doc.Close
    wrd.Quit
    Set doc = Nothing
    Set wrd = Nothing
I've never done a merge from a RTB myself so this was an alternative. I will have to look into using an RTB to a mail merge. Maybe kimpeterson could offer a solution rather than just commenting?
 
Just for curiosity I have now done a mail merge in Word, and I'v ran it in word to make sure it works. However if I use the code sugested all I get is the Field names, Im obviously missing something here. This is something new to me so please be patient.
 
You need to make sure that in word you have selected to show the merged details and not the fields. On your mail merge toolbar in word you will have an icon that looks a bit like <<ABC>>. This will change between the fields and the data.
 
Your being very helpful ca8msm, I do appreciate it. I am now getting the correct address printing out depending on the SQL, but there is only one page. If I run merge from word it comes up with the correct number of pages after I run the VB application.

Coding problem or something in word?.
 
I think you will need to loop through each record. I think you can cycle through the records using:
Code:
ActiveDocument.MailMerge.DataSource.ActiveRecord =wdNextRecord
although you will have to check for the first and last records to make sure you don't duplicate prints or try to go past the last record.
 
Its now looping through the records but not sure how to check for the last record, this is what I have so far :-

Do While ActiveDocument.MailMerge.DataSource.ActiveRecord _ <> wdLastRecord
doc.PrintOut True
Do While wrd.BackgroundPrintingStatus <> 0
Loop
ActiveDocument.MailMerge.DataSource.ActiveRecord _ = wdNextRecord
Loop
 
Hi Denster,

I am new VB user.

Now I am doing mail merge using VB6 but I could not use the data pull from oracle merge to main word document?

I tried to use the code segements in your posting but when I compile it, I got error (invalid use of property)
for this statement:
ActiveDocument.MailMerge.DataSource.ActiveRecord

Would you mind to share with me your code if you done with this? I am really frastrated with this merge things in VB6. Please help. Your kind help will be greatly appreciated.

Thanks in advance!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top