473,387 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Changing Mailmerge DataSource

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").Column s
(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").Column s.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").Column s(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
Nov 20 '05 #1
0 1575

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Allcomp | last post by:
Hello, I have a problem with a VB6 program that command a mailmerge with word object. The database is a MSDE Database. In the two cases, the server is the same The file c:\res.odc is empty...
0
by: Bud Zielinski | last post by:
Some of our users have converted to Office 2003. Our Access app is in 2000. they run it in Access 2003 with Access 2000 as the default file format. When doing a merge from the partial code...
1
by: Bud Zielinski | last post by:
Some of our users have converted to Office 2003. Our Access app is in 2000. they run it in Access 2003 with Access 2000 as the default file format. When doing a merge from the partial code...
2
by: Shannon Rotz | last post by:
I have a function in a Microsoft Access module (below) which creates a Word XP Mailmerge document, drawing the data from Access XP. I can get the connection to work fine. However, when I create...
0
by: li.shen | last post by:
Hi, I am trying to use mailmerge to send out emails to a list of people in my datasource. Is there anyway I could use the field value from the datasource to be my email subjects, so that I have...
1
by: Karen Hill | last post by:
How do I do a mailmerge via automation in Access? I would like to have the datasource be an SQL statment. I would like this to be in VB without using MS Query. Thanks in advance Access experts...
10
by: John | last post by:
Hi I am trying to do a word mailmerge form within my vb.net app. My problem is how to do a query on one of my tables and use the result as the mail merge datasource. Any help would be...
9
by: Chubbly Geezer | last post by:
Hope you can help. I have created a mail merge word doc which seems to work fine. When I close and reload it asks if I wish to pull in the data. Great. However, I want to print the results of...
4
by: Willis Wilson | last post by:
Platform: Access 2003 SP2 and Windows XP. I've tried a number of different scenarios and none of them seem to work The "OpenDataSource" statement gives a Run-time error 5922. Please help find...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.