Hi,
We are supposed to create a Word doc file for our client that is to be used for printing a large list of names and addresses.
I am facing some problem at the time of retrieving the data from MS-Access and showing it inthe Ms-Word Document [here I am using C#.net Windows Application].
The below is the code for your reference:
private void button1_Click(object sender, System.EventArgs e)
{
try
{
button1.Enabled = false;
StreamWriter sw;
DateTime dt = DateTime.Now;
string strFileName = @"Rene's book .doc";
string strMyOdbc = "SELECT * FROM Contacts ";
strMyOdbc += "ORDER BY Account, LastName";
OdbcConnection myConnection = new OdbcConnection(strOleDbConnection);
OdbcDataAdapter myOdbcAdapter = new OdbcDataAdapter(strMyOdbc, myConnection);
DataSet dsContacts = new DataSet();
OdbcCommandBuilder mySqlCommandBuilder = new OdbcCommandBuilder(myOdbcAdapter);
myOdbcAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
myOdbcAdapter.Fill(dsContacts, "Contacts");
int iTotalCnt = 0;
string strNotes = "";
if (dsContacts.Tables.Count > 0)
{
iTotalCnt = dsContacts.Tables[0].Rows.Count;
using (sw= new StreamWriter(strFileName, false, System.Text.Encoding.Default))
{
sw.NewLine = clsVB.Chr(11);
foreach (DataRow myRow in dsContacts.Tables[0].Rows)
{
if ("" != myRow["FirstName"].ToString() || "" != myRow["LastName"].ToString())
{
if ("" != myRow["Title"].ToString())
{
sw.Write(myRow["Title"].ToString() + " ");
}
if ("" != myRow["FirstName"].ToString())
{
sw.Write(myRow["FirstName"].ToString() + " ");
}
if ("" != myRow["LastName"].ToString())
{
sw.Write(myRow["LastName"].ToString());
}
sw.WriteLine();
}
if ("" != myRow["JobTitle"].ToString())
{
sw.Write(myRow["JobTitle"].ToString());
sw.WriteLine();
}
if ("" != myRow["Company"].ToString())
{
sw.Write(myRow["Company"].ToString());
sw.WriteLine();
}
if ("" != myRow["BusinessStreet"].ToString())
{
sw.Write(myRow["BusinessStreet"].ToString());
sw.WriteLine();
}
if ("" != myRow["BusinessStreet2"].ToString())
{
sw.Write(myRow["BusinessStreet2"].ToString());
sw.WriteLine();
}
if ("" != myRow["BusinessCity"].ToString() || "" != myRow["BusinessState"].ToString() || "" != myRow
["BusinessPostalCode"].ToString())
{
if ("" != myRow["BusinessCity"].ToString())
{
sw.Write(myRow["BusinessCity"].ToString() + ", ");
}
if ("" != myRow["BusinessState"].ToString())
{
sw.Write(myRow["BusinessState"].ToString() + " ");
}
if ("" != myRow["BusinessPostalCode"].ToString())
{
sw.Write(myRow["BusinessPostalCode"].ToString());
}
sw.WriteLine();
}
if ("" != myRow["BusinessPhone"].ToString())
{
sw.Write("Business Phone 1: " + myRow["BusinessPhone"].ToString());
sw.WriteLine();
}
if ("" != myRow["BusinessPhone2"].ToString())
{
sw.Write("Business Phone 2: " + myRow["BusinessPhone2"].ToString());
sw.WriteLine();
}
if ("" != myRow["MobilePhone"].ToString())
{
sw.Write("Mobile Phone: " + myRow["MobilePhone"].ToString());
sw.WriteLine();
}
if ("" != myRow["Pager"].ToString())
{
sw.Write("Pager: " + myRow["Pager"].ToString());
sw.WriteLine();
}
if ("" != myRow["OtherFax"].ToString())
{
sw.Write("Fax: " + myRow["OtherFax"].ToString());
sw.WriteLine();
}
if ("" != myRow["BusinessFax"].ToString())
{
sw.Write("Office Fax: " + myRow["BusinessFax"].ToString());
sw.WriteLine();
}
if ("" != myRow["EmailAddress"].ToString())
{
sw.Write("Primary Email: " + myRow["EmailAddress"].ToString());
sw.WriteLine();
}
if ("" != myRow["Email2Address"].ToString())
{
sw.Write("Secondary Email: " + myRow["Email2Address"].ToString());
sw.WriteLine();
}
if ("" != myRow["AssistantsName"].ToString())
{
sw.Write("Assistant: " + myRow["AssistantsName"].ToString());
sw.WriteLine();
}
if ("" != myRow["User1"].ToString() || "" != myRow["User2"].ToString())
{
if ("" != myRow["User1"].ToString())
{
sw.Write(myRow["User1"].ToString() + " ");
}
if ("" != myRow["User2"].ToString())
{
sw.Write(myRow["User2"].ToString());
}
sw.WriteLine();
}
if ("" != myRow["Notes"].ToString())
{
strNotes = myRow["Notes"].ToString();
if (strNotes.Length > 1)
{
strNotes = strNotes.Substring(0, strNotes.Length - 1);
}
sw.Write(strNotes);
sw.WriteLine();
}
sw.Write(clsVB.Chr(10));
//sw.WriteLine();
}
label1.Text = iTotalCnt.ToString() + " contacts created in Rene's book";
}
}
button1.Enabled = true;
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
button1.Enabled = true;
}
}
The above code is retrieving the values from the Database and populating it in the word document without any problem.
We get a funny Unicode message when opening up the document. How can I avoid the Unicode File conversion wizard at the time of opening up the document? Could you please giveyour valuable suggestions as soon as possible?
Thanks and Regards---Madhu