PC Review


Reply
Thread Tools Rate Thread

Changing Mailmerge DataSource

 
 
Bob Smith
Guest
Posts: n/a
 
      2nd Aug 2004
I have existing word documents that we need to have the
data document reflect whether we're running in
development, test or production.

The data header is longer than 255 characters, so I cannot
put the structure in either createdatasource or
opendatasource so I have to build it on the fly.

The code I have written is as follows:

Try
sSql = "select * from ordersdatatable"
dsOrdersDataTable =
lungTransplantData.GetDataSet(sSql, "ordersdatatable")
Catch ex As Exception
MessageBox.Show("Failure to create
OrdersDataTable in merge document.")
End Try

Dim sHeader As String = ""
Dim x As Integer

For x = 0 To dsOrdersDataTable.Tables
("ordersdatatable").Columns.Count - 1
If x = 1 Then
sHeader = dsOrdersDataTable.Tables
("ordersdatatable").Columns(x).ColumnName
Else
sHeader = sHeader & "," &
dsOrdersDataTable.Tables("ordersdatatable").Columns
(x).ColumnName
End If
Next

With Wordobj.ActiveDocument
If .MailMerge.State =
Word.WdMailMergeState.wdMainAndDataSource Then
' We cannot use a connection
string here from the application due to compatibility with
Word.
.MailMerge.CreateDataSource
("OrdersDataTable.Doc", , , sHeader, , , , , True)

' Open the file to insert data.
wrdDataDoc = Wordobj.Documents.Open
("OrdersDataTable.doc")
wrdDataDoc.PageSetup.PaperSize =
Word.WdPaperSize.wdPaperLegal
wrdDataDoc.PageSetup.Orientation =
Word.WdOrientation.wdOrientLandscape
wrdDataDoc.Tables.Item(1).Rows.Add
()

Dim i As Integer
Dim OpenWordDoc As Word.Document

With wrdDataDoc.Tables.Item(1)
.AllowAutoFit = False
.Columns.Width = 15

For i = 1 To
dsOrdersDataTable.Tables("OrdersDataTable").Columns.Count
' Insert the data in the
specific cell.
If .Columns.Count < i - 1
Then
.Columns.Add()
End If
.Cell(1, i - 1).Range.Text
= dsOrdersDataTable.Tables("OrdersDataTable").Columns(i -
1).ColumnName
If Not IsDBNull
(dsOrdersDataTable.Tables("OrdersDataTable").Rows(0).Item
(i - 1)) Then
.Cell(2, i -
1).Range.Text = dsOrdersDataTable.Tables
("OrdersDataTable").Rows(0).Item(i - 1)
Else
.Cell(2, i -
1).Range.Text = ""
End If
Next
End With

.MailMerge.Destination =
Word.WdMailMergeDestination.wdSendToNewDocument
.MailMerge.Execute()
End If
End With


This all works pretty well with two exceptions:
1) wrdDataDoc winds up being the frontmost form....how do
I move it to the back or how can I close it without
affecting the main document.
2) Sometimes I receive a "Invalid Merge Field" dialog to
prompt for changes and then when I cancel, it works anyway.

Any suggestions will be much appreciated.

Thanks,

Bob Smith
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mailmerge with excal as datasource Claus Persson Microsoft Excel Programming 1 29th Jul 2008 03:40 PM
SELECT UNIQUE some_column FROM MAILMERGE.DATASOURCE L=?ISO-8859-1?B?/A==?=ko Willms Microsoft Excel Programming 6 16th Nov 2006 12:12 PM
mailmerge function not opening datasource =?Utf-8?B?RG93bmluZ0RldmVsb3BtZW50cw==?= Microsoft Access External Data 1 5th Apr 2006 02:14 PM
MailMerge from Excel 2000 using self as datasource =?Utf-8?B?QW5keUI=?= Microsoft Excel Programming 0 7th Mar 2005 02:35 PM
Mailmerge to Outlook from Word datasource =?Utf-8?B?QWJieUxU?= Microsoft Outlook Discussion 1 7th Dec 2004 02:56 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:01 PM.