How can I set the default Font Style to TimesNewRoman-11 in C#.net Windows Application  
Author Message
Madhu Reddy





PostPosted: Visual C# General, How can I set the default Font Style to TimesNewRoman-11 in C#.net Windows Application Top

Hello All,

I am retrieving the values from the Access Databse and populating in the Ms Word document using the following code.

private void button1_Click(object sender, System.EventArgs e)
{
try
{

button1.Enabled = false;
StreamWriter sw;
DateTime dt = DateTime.Now;



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().Trim())
{
if ("" != myRow["Title"].ToString())
{
sw.Write(myRow["Title"].ToString()+ " ");

}
if ("" != myRow["FirstName"].ToString().Trim())
{
sw.Write(myRow["FirstName"].ToString().Replace(" ", String.Empty) + " ");
}

if ("" != myRow["LastName"].ToString())
{
sw.Write(myRow["LastName"].ToString().Replace(" ", String.Empty));
}

sw.WriteLine();
}

if ("" != myRow["JobTitle"].ToString())
{
sw.Write(myRow["JobTitle"].ToString().Replace(" ", String.Empty));
sw.WriteLine();
}
if ("" != myRow["Company"].ToString())
{
sw.Write(myRow["Company"].ToString().Replace(" ", String.Empty));
sw.WriteLine();
}
if ("" != myRow["BusinessStreet"].ToString())
{
sw.Write(myRow["BusinessStreet"].ToString().Replace(" ", String.Empty));
sw.WriteLine();
}
if ("" != myRow["BusinessStreet2"].ToString())
{
sw.Write(myRow["BusinessStreet2"].ToString().Replace(" ", String.Empty));
sw.WriteLine();
}
if ("" != myRow["BusinessCity"].ToString() || "" != myRow["BusinessState"].ToString() || "" != myRow["BusinessPostalCode"].ToString())
{
if ("" != myRow["BusinessCity"].ToString())
{
sw.Write(myRow["BusinessCity"].ToString().Replace(" ", String.Empty) + ", ");
}
if ("" != myRow["BusinessState"].ToString())
{
sw.Write(myRow["BusinessState"].ToString().Replace(" ", String.Empty) + " ");
}
if ("" != myRow["BusinessPostalCode"].ToString())
{
sw.Write(myRow["BusinessPostalCode"].ToString().Replace(" ", String.Empty));
}
sw.WriteLine();
}
if ("" != myRow["BusinessPhone"].ToString())
{
sw.Write("Business Phone 1: " + myRow["BusinessPhone"].ToString().Replace(" ", String.Empty));
sw.WriteLine();
}
if ("" != myRow["BusinessPhone2"].ToString())
{
sw.Write("Business Phone 2: " + myRow["BusinessPhone2"].ToString().Replace(" ", String.Empty));
sw.WriteLine();
}
if ("" != myRow["MobilePhone"].ToString())
{
sw.Write("Mobile Phone: " + myRow["MobilePhone"].ToString().Replace(" ", String.Empty));
sw.WriteLine();
}
if ("" != myRow["Pager"].ToString())
{
sw.Write("Pager: " + myRow["Pager"].ToString().Replace(" ", String.Empty));
sw.WriteLine();
}
if ("" != myRow["OtherFax"].ToString())
{
sw.Write("Fax: " + myRow["OtherFax"].ToString().Replace(" ", String.Empty));
sw.WriteLine();
}
if ("" != myRow["BusinessFax"].ToString())
{
sw.Write("Office Fax: " + myRow["BusinessFax"].ToString().Replace(" ", String.Empty));
sw.WriteLine();
}
if ("" != myRow["EmailAddress"].ToString())
{
sw.Write("Primary Email: " + myRow["EmailAddress"].ToString().Replace(" ", String.Empty));
sw.WriteLine();
}
if ("" != myRow["Email2Address"].ToString())
{
sw.Write("Secondary Email: " + myRow["Email2Address"].ToString().Replace(" ", String.Empty));
sw.WriteLine();
}
if ("" != myRow["AssistantsName"].ToString())
{
sw.Write("Assistant: " + myRow["AssistantsName"].ToString().Replace(" ", String.Empty));
sw.WriteLine();
}
if ("" != myRow["User1"].ToString() || "" != myRow["User2"].ToString())
{
if ("" != myRow["User1"].ToString())
{
sw.Write(myRow["User1"].ToString().Replace(" ", String.Empty) + " ");
}
if ("" != myRow["User2"].ToString())
{
sw.Write(myRow["User2"].ToString().Replace(" ", String.Empty));
}
sw.WriteLine();
}
if ("" != myRow["Notes"].ToString().Trim())
{
strNotes = myRow["Notes"].ToString();
if (strNotes.Length > 1)
{
//strNotes = strNotes.Substring(0, strNotes.Length - 1).Replace(" ", String.Empty);
strNotes = strNotes.Substring(0, strNotes.Length - 1).Replace(" ", String.Empty).Trim();
}
sw.Write(strNotes);
sw.WriteLine();
}
//sw.WriteLine();
sw.Write(clsVB.Chr(10));

//sw.WriteLine();

}
label1.Text = iTotalCnt.ToString() + " contacts created in Rene's book";
sw.Close();
sw = null;
}
}
button1.Enabled = true;
}

catch (Exception exc)
{
MessageBox.Show(exc.Message);
button1.Enabled = true;
}
}

Using this code i am populating the values from Access Database to MS word. By default the font size is showing "Courier". Our requirement is "TimesNewRoman-11".How can i Set this

Could you please give your valuable suggestions as soon as possible.

Thanks and Regards---Madhu



Visual C#17