S
Shalini
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
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