mailmerge; datasource

B

Bouwman

Hi guys,
(vb6)
I want to present to the user a blank word document with a known
datasource àttached` to that document.
We tried the code as shown below. The effect of that code is that it
tries to perform the mailmerge. That is not what we want. We can't find
an `addDatasource` statement or something to that effect.

Thanks in advance,
Peter



********** CODE ***************
Dim MyMailMerge As Object 'MailMerge
Dim oDoc As Object
Dim WVersion As String

WVersion = cpWordApp.Version
cpWordApp.Visible = cpWordVisibleJN

Set oDoc = cpWordApp.Documents.Add
Set MyMailMerge = oDoc.MailMerge

If WVersion = "9.0" Then
'Office 2000
MyMailMerge.OpenDataSource Name:="", _
LinkToSource:=True, AddToRecentFiles:=False, _
Connection:=cpConnStr, SQLStatement:="select * from selektie"
End If

If WVersion = "10.0" Then
'Office XP
MyMailMerge.OpenDataSource Name:="", _
LinkToSource:=True, AddToRecentFiles:=False, _
Connection:=cpConnStr, SQLStatement:="select * from selektie", _
SubType:=wdMergeSubTypeWord2000
End If

Set MyMailMerge = Nothing
Set oDoc = Nothing
 
P

Peter Hewett

Hi Bouwman

I'll answer the questions using VBA rather than VB. The only difference is
of course the Word application object you'll need to prefix certain object
references with.

You may not even need much code. If you're using an existing
document/template for the MailMerge then you can manually set that up as
your MainDocument and attach the appropriate data source.

In either case, before you can attach a data source you need to set the
document as the MailMerge MainDocument (the document you insert the
MailMerge fields into). Once you've done this you can set the MailMerge
DataSource.

The code as follows (Word 2k) sets the current document to be the
MailMerges MainDocument and then sets the DataSource to a word document
called "Mail Merge Data Source.doc":

Sub SetupMailMerge()

ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
ActiveDocument.MailMerge.OpenDataSource Name:= _
"F:\X\Mail Merge Data Source.doc", ConfirmConversions:=False, _
ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="", WritePasswordTemplate:="", _
Revert:=False, Format:=wdOpenFormatAuto, Connection:="", _
SQLStatement:="", SQLStatement1:=""
End Sub

HTH + Cheers - Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top