Click here to Skip to main content
15,898,749 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,

My Code:
VB
Dim wrdMailMerge As Word.MailMerge
myApp = CreateObject("Word.Application")
myDoc = myApp.Documents.Open("DOC. PATH")
wrdMailMerge = myDoc.MailMerge
wrdMailMerge.MainDocumentType = Word.WdMailMergeMainDocType.wdFormLetters
wrdMailMerge.Execute()
myApp.ActiveDocument.SaveAs("DOC. PATH")

wrdMailMerge.Execute() in this line throwing Error given below,

This method or property is not available because the current mail merge main document needs a data source.

I want to connect mail merge without DSN.

pls help me out.

Regards
Rajesh V
Posted
Updated 23-Apr-13 23:22pm
v3

I've personally never done this before, but just from reading the error message it sounds like you need to set a datasource. If you were going to manually mail merge a document in Word, you'd have to provide a datasource for it. For example, if your word document was a letter and you wanted to mail merge to a comma delimited text file that contained names and addresses, your data source would be the .txt file.

You can find some general help just from google[^].

The first result here[^] has a code sample.
VB
objWord.ActiveDocument.MailMerge.OpenDataSource("YOUR DATABASE PATH", _
               Connection:="dsn=DSN NAME; dbq=" & YOUR DATABASE PATH & ";", _
               sqlstatement:="select * from `" & tableName & "`")

This person appears to be using a database (not a textfile like in the scenario I hypothetically made above) so you'll have to research how to connect to whatever source you are using.

Hope this helps.
 
Share this answer
 
Comments
Rajesh Vetrivel 24-Apr-13 5:20am    
Thanks for your reply Kschuler
I want Without DSN Connect Mail Merge using vb.net

Regards
Rajesh V
Kschuler 24-Apr-13 9:20am    
http://www.google.com/#hl=en&sclient=psy-ab&q=vb.net+mailmerge+word+without+dsn&oq=vb.net+mailmerge+word+without+dsn&gs_l=hp.3...824.9897.0.10160.45.28.10.6.6.0.206.3723.1j26j1.28.0...0.0...1c.1.11.psy-ab.AerALI63OyY&pbx=1&bav=on.2,or.r_qf.&bvm=bv.45580626,d.eWU&fp=f845baf05245cb05&biw=1152&bih=734
VB
Dim objWord As New Object
objWord = CreateObject("Word.Application") ' Creating a word application
objWord.application.WindowState = 0 ' set the word window in normal state (Const wdWindowStatENormal = 0)
objWord.Documents.Open(System.AppDomain.CurrentDomain.BaseDirectory() & "\WORD.doc")
objWord.ActiveDocument.MailMerge.OpenDataSource( _
Name:=System.AppDomain.CurrentDomain.BaseDirectory() & "ACCESSDB.mdb", _
Connection:="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ACCESSDB.mdb;Mode=Read;", _
SQLStatement:="select * from `TABLE_NAME`", _
SubType:=Word.WdMergeSubType.wdMergeSubTypeAccess)
objWord.ActiveDocument.MailMerge.Execute()
objWord.ActiveDocument.SaveAs(System.AppDomain.CurrentDomain.BaseDirectory() & "\WORD1.DOC")
objWord.Parent.Windows(2).Close(Word.WdSaveOptions.wdDoNotSaveChanges)
objWord.Visible = True
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900