Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Mailmerge and Fax / June 2005

Tip: Looking for answers? Try searching our database.

"Word was unable to open the Excel data source" Asp.net Mailmerge

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Shalini - 17 Jun 2005 11:14 GMT
Hi,

I am trying to perform word mail merge operation via ASP.Net using Excel as
data source.

Code is attached below:
Dim wrdApp As Word.Application
       Dim wrdDoc As Word._Document
       Dim wrdMailMerge As Word.MailMerge
       Dim fileWORDName, fileEXCELName As Object

       Try
           wrdApp = New Word.Application

           
wrdApp.ChangeFileOpenDirectory("C:\Inetpub\wwwroot\FPA\Templates\")
           fileWORDName = "ThankYou-Shalini.doc"

           wrdDoc = wrdApp.Documents.Open(FileName:=fileWORDName,
ConfirmConversions:=False, ReadOnly _
           :=False, AddToRecentFiles:=False, PasswordDocument:="",
PasswordTemplate _
           :="", Revert:=False, WritePasswordDocument:="",
WritePasswordTemplate:="" _
           , Format:=Word.WdOpenFormat.wdOpenFormatAuto)

           wrdMailMerge = wrdDoc.MailMerge
           wrdMailMerge.MainDocumentType =
Word.WdMailMergeMainDocType.wdNotAMergeDocument

           wrdApp.Visible = True
           wrdDoc.ActiveWindow.Activate()
           wrdDoc.ActiveWindow.Visible = True

         
  wrdApp.ChangeFileOpenDirectory("C:\Inetpub\wwwroot\FPA\Archive\")
           fileEXCELName = "FullfillmentReport1.xls"

           wrdMailMerge.OpenDataSource(Name:= _
               fileEXCELName, _
               ConfirmConversions:=False, ReadOnly:=False,
LinkToSource:=True, _
               AddToRecentFiles:=False, PasswordDocument:="",
PasswordTemplate:="", _
               WritePasswordDocument:="", WritePasswordTemplate:="",
Revert:=False, _
               Format:=Word.WdOpenFormat.wdOpenFormatAuto, Connection:= _
               "DSN=Excel
Files;DBQ=fileEXCELName;DriverId=790;MaxBufferSize=2048;PageTimeout=5;" _
               , SQLStatement:="SELECT * FROM 'Data'", SQLStatement1:="")
         

           ' Perform mail merge.
           wrdMailMerge.Destination =
Word.WdMailMergeDestination.wdSendToNewDocument
           wrdMailMerge.MainDocumentType =
Word.WdMailMergeMainDocType.wdFormLetters

           wrdMailMerge.SuppressBlankLines = True

           wrdMailMerge.DataSource.FirstRecord =
Word.WdMailMergeDefaultRecord.wdDefaultFirstRecord
           wrdMailMerge.DataSource.LastRecord =
Word.WdMailMergeDefaultRecord.wdDefaultLastRecord

           wrdMailMerge.Execute(False)

           ' Close the original form document.
           wrdDoc.Saved = True
           wrdDoc.Close(False)

       Catch ex As Exception
           Response.Write(ex.ToString)
           Dim AllWORDProcess() As System.Diagnostics.Process =
System.Diagnostics.Process.GetProcessesByName("WINWORD")
           Dim WordProcess As New System.Diagnostics.Process
           For Each WordProcess In AllWORDProcess
               WordProcess.Kill()
           Next
           WordProcess.Close()

           Dim AllEXCELProcess() As System.Diagnostics.Process =
System.Diagnostics.Process.GetProcessesByName("Excel")
           Dim ExcelProcess As New System.Diagnostics.Process
           For Each ExcelProcess In AllEXCELProcess
               ExcelProcess.Kill()
           Next
           ExcelProcess.Close()
       Finally
           ' Release References.
           wrdMailMerge = Nothing
           wrdDoc = Nothing
           wrdApp = Nothing
       End Try

Please do the needful
Doug Robbins - 17 Jun 2005 12:29 GMT
Looking at your code, it appears that the initially, MainDocumentType is
being set to wdNotAMergeDocument and that is probably the reason that you
are getting the error message.

You do later change the MainDocumentType, but I think you will have to do
that before you attempt to attach the data source.

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> Hi,
>
[quoted text clipped - 90 lines]
>
> Please do the needful
Shalini - 17 Jun 2005 12:53 GMT
Thanks for your prompt reply.

But hard luck, even after commenting that statement, i am still getting the
same error.

System.Runtime.InteropServices.COMException (0x800A1722): Word was unable to
open the data source. at Word.MailMerge.OpenDataSource(String Name, Object&
Format, Object& ConfirmConversions, Object& ReadOnly, Object& LinkToSource,
Object& AddToRecentFiles, Object& PasswordDocument, Object& PasswordTemplate,
Object& Revert, Object& WritePasswordDocument, Object& WritePasswordTemplate,
Object& Connection, Object& SQLStatement, Object& SQLStatement1) at
FPA.JobIntegration.btnMailMerge_Click(Object sender, EventArgs e) in
C:\Inetpub\wwwroot\FPA\JobIntegration.aspx.vb:line 88

Please do the needful.

> Looking at your code, it appears that the initially, MainDocumentType is
> being set to wdNotAMergeDocument and that is probably the reason that you
[quoted text clipped - 97 lines]
> >
> > Please do the needful
Matt - 23 Jun 2005 02:09 GMT
Hi Shanli,

I needed to do a merge in a windows forms app and used your code. I made a
change to the excel datasource and that was about it... It seems to work for
me with office 2003. If you have any other enhancements, please post them.

Regards,

Matt

Dim wrdApp As Word.Application
       Dim wrdDoc As Word._Document
       Dim wrdDoc1 As Word.MailMergeDataSource
       Dim wrdSelection As Word.Selection
       Dim wrdMailMerge As Word.MailMerge
       Dim wrdMergeFields As Word.MailMergeFields
       Dim fileWORDName, fileEXCELName As String

       Try
           wrdApp = New Word.Application

           fileWORDName = "C:\merge.doc"

           wrdDoc = wrdApp.Documents.Open(FileName:=fileWORDName,
ConfirmConversions:=False, ReadOnly _
           :=False, AddToRecentFiles:=False, PasswordDocument:="",
PasswordTemplate _
           :="", Revert:=False, WritePasswordDocument:="",
WritePasswordTemplate:="" _
           , Format:=Word.WdOpenFormat.wdOpenFormatAuto)

           wrdMailMerge = wrdDoc.MailMerge
           wrdMailMerge.MainDocumentType =
Word.WdMailMergeMainDocType.wdFormLetters

           wrdApp.Visible = False
           wrdDoc.ActiveWindow.Activate()
           wrdDoc.ActiveWindow.Visible = True

           wrdMailMerge.OpenDataSource(Name:="c:\TestMerge.xls",
SQLStatement:="SELECT * FROM `Results$`")  'Don't forget the Sheetname

           ' Perform mail merge.
           wrdMailMerge.MainDocumentType =
Word.WdMailMergeMainDocType.wdFormLetters
           wrdMailMerge.Destination =
Word.WdMailMergeDestination.wdSendToNewDocument
           wrdMailMerge.SuppressBlankLines = True
           wrdMailMerge.DataSource.FirstRecord =
Word.WdMailMergeDefaultRecord.wdDefaultFirstRecord
           wrdMailMerge.DataSource.LastRecord =
Word.WdMailMergeDefaultRecord.wdDefaultLastRecord
           wrdMailMerge.Execute(True)

           ' Close the original form document.
           wrdDoc.Saved = True
           wrdDoc.Close(False)

       Catch ex As Exception
           MessageBox.Show(ex.ToString)
           Dim AllWORDProcess() As System.Diagnostics.Process =
System.Diagnostics.Process.GetProcessesByName("WINWORD")
           Dim WordProcess As New System.Diagnostics.Process
           For Each WordProcess In AllWORDProcess
               WordProcess.Kill()
           Next
           WordProcess.Close()

           Dim AllEXCELProcess() As System.Diagnostics.Process =
System.Diagnostics.Process.GetProcessesByName("Excel")
           Dim ExcelProcess As New System.Diagnostics.Process
           For Each ExcelProcess In AllEXCELProcess
               ExcelProcess.Kill()
           Next
           ExcelProcess.Close()
       Finally
           wrdMailMerge = Nothing
           wrdDoc = Nothing
           wrdApp = Nothing
       End Try

> Thanks for your prompt reply.
>
[quoted text clipped - 113 lines]
> > >
> > > Please do the needful
Peter Jamieson - 17 Jun 2005 13:13 GMT
You do not say what is going wrong and at what point it is happening, but...

> Dim fileWORDName, fileEXCELName As Object

I do not know which is required or better in ASP.NET, but can these be "As
String". Does it make any difference if they are?

>   wrdDoc = wrdApp.Documents.Open(FileName:=fileWORDName,

If this document is already a Mail Merge Main Document, Word will look for
its data source when OpenFileName executes. If it cannot find the data
source, Word will try to display a dialog box, so will probably hang in your
situation. You cannot prevent this, so the only good solution is to be
certain that the document is not attached to a data source before your
ASP.NET routine tries to open it.

>   wrdApp.ChangeFileOpenDirectory("C:\Inetpub\wwwroot\FPA\Archive\")
>   fileEXCELName = "FullfillmentReport1.xls"

>   wrdMailMerge.OpenDataSource(Name:= _
>      fileEXCELName, _

This /may/ work, but I would try

fileEXCELName = "C:\Inetpub\wwwroot\FPA\Archive\FullfillmentReport1.xls"

> Connection:= _
> "DSN=Excel
> Files;DBQ=fileEXCELName;DriverId=790;MaxBufferSize=2048;PageTimeout=5;" _

The DBQ must be the name of the file. here, you are using a piece of text
"fileEXCELName". Try something like

Connection:= _
"DSN=Excel Files;DBQ=" & fileEXCELName &
";DriverId=790;MaxBufferSize=2048;PageTimeout=5;" _

I suspect the fileEXCELName will have to be the full path name of the file
(as above) for this to work.

> SQLStatement:="SELECT * FROM 'Data'"

This may be OK, but
a. it clearly requires that your Excel workbook has a named range called
Data
b. you /may/ need to use a different kind of single quotes around Data,
i.e. single back quotes:

SQLStatement:="SELECT * FROM `Data`"

Try those changes first. and let's see...

Peter Jamieson

From: "Shalini" <Shalini @discussions.microsoft.com>
Subject: "Word was unable to open the Excel data source" Asp.net Mailmerge
Date: 17 June 2005 11:14

Hi,

I am trying to perform word mail merge operation via ASP.Net using Excel as
data source.

Code is attached below:
Dim wrdApp As Word.Application
       Dim wrdDoc As Word._Document
       Dim wrdMailMerge As Word.MailMerge
       Dim fileWORDName, fileEXCELName As Object

       Try
           wrdApp = New Word.Application

wrdApp.ChangeFileOpenDirectory("C:\Inetpub\wwwroot\FPA\Templates\")
           fileWORDName = "ThankYou-Shalini.doc"

           wrdDoc = wrdApp.Documents.Open(FileName:=fileWORDName,
ConfirmConversions:=False, ReadOnly _
           :=False, AddToRecentFiles:=False, PasswordDocument:="",
PasswordTemplate _
           :="", Revert:=False, WritePasswordDocument:="",
WritePasswordTemplate:="" _
           , Format:=Word.WdOpenFormat.wdOpenFormatAuto)

           wrdMailMerge = wrdDoc.MailMerge
           wrdMailMerge.MainDocumentType =
Word.WdMailMergeMainDocType.wdNotAMergeDocument

           wrdApp.Visible = True
           wrdDoc.ActiveWindow.Activate()
           wrdDoc.ActiveWindow.Visible = True

  wrdApp.ChangeFileOpenDirectory("C:\Inetpub\wwwroot\FPA\Archive\")
           fileEXCELName = "FullfillmentReport1.xls"

           wrdMailMerge.OpenDataSource(Name:= _
               fileEXCELName, _
               ConfirmConversions:=False, ReadOnly:=False,
LinkToSource:=True, _
               AddToRecentFiles:=False, PasswordDocument:="",
PasswordTemplate:="", _
               WritePasswordDocument:="", WritePasswordTemplate:="",
Revert:=False, _
               Format:=Word.WdOpenFormat.wdOpenFormatAuto, Connection:= _
               "DSN=Excel
Files;DBQ=fileEXCELName;DriverId=790;MaxBufferSize=2048;PageTimeout=5;" _
               , SQLStatement:="SELECT * FROM 'Data'", SQLStatement1:="")

           ' Perform mail merge.
           wrdMailMerge.Destination =
Word.WdMailMergeDestination.wdSendToNewDocument
           wrdMailMerge.MainDocumentType =
Word.WdMailMergeMainDocType.wdFormLetters

           wrdMailMerge.SuppressBlankLines = True

           wrdMailMerge.DataSource.FirstRecord =
Word.WdMailMergeDefaultRecord.wdDefaultFirstRecord
           wrdMailMerge.DataSource.LastRecord =
Word.WdMailMergeDefaultRecord.wdDefaultLastRecord

           wrdMailMerge.Execute(False)

           ' Close the original form document.
           wrdDoc.Saved = True
           wrdDoc.Close(False)

       Catch ex As Exception
           Response.Write(ex.ToString)
           Dim AllWORDProcess() As System.Diagnostics.Process =
System.Diagnostics.Process.GetProcessesByName("WINWORD")
           Dim WordProcess As New System.Diagnostics.Process
           For Each WordProcess In AllWORDProcess
               WordProcess.Kill()
           Next
           WordProcess.Close()

           Dim AllEXCELProcess() As System.Diagnostics.Process =
System.Diagnostics.Process.GetProcessesByName("Excel")
           Dim ExcelProcess As New System.Diagnostics.Process
           For Each ExcelProcess In AllEXCELProcess
               ExcelProcess.Kill()
           Next
           ExcelProcess.Close()
       Finally
           ' Release References.
           wrdMailMerge = Nothing
           wrdDoc = Nothing
           wrdApp = Nothing
       End Try

Please do the needful
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.