[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Use Microsoft Word to make a timezone conversion chart in C#

timezone conversion chart

This example uses Word to generate a timezone conversion chart that lets you easily convert hours from one timezone to another. Select a date and the two timezones. Then press Make Chart to make the conversion chart. (You need to select a date because time differences between timezones vary depending on Daylight Savings schedules.)

The program uses Microsoft Word's object library, so you need to include a reference to it. In Solution Explorer, right-click References and select Add Reference. Click the COM tab, select the "Microsoft Word 14.0 Object Library" entry (or whatever version is installed on your computer), and click OK.

This example is a bit long. Most of its code creates the Word document. If you want to skip over that part, just look at the blue code below.

// Make a conversion chart. private void btnMakeChart_Click(object sender, EventArgs e) { // Get the timezone information. TimeZoneInfo zone1 = cboTimeZone1.SelectedItem as TimeZoneInfo; TimeZoneInfo zone2 = cboTimeZone2.SelectedItem as TimeZoneInfo; // Get the Word application object. Word._Application word_app = new Word.Application(); // Make Word visible (optional). word_app.Visible = true; // Create the Word document. object missing = Type.Missing; Word._Document word_doc = word_app.Documents.Add( ref missing, ref missing, ref missing, ref missing); // Create a table. object start = 0; Word.Range range = word_doc.Range(ref start, ref start); object collapse_end = Word.WdCollapseDirection.wdCollapseEnd; range.Collapse(ref collapse_end); object default_table_behavior = Word.WdDefaultTableBehavior.wdWord9TableBehavior; object auto_fit_behavior = Word.WdAutoFitBehavior.wdAutoFitContent; Word.Table word_table = word_doc.Tables.Add( range, 25, 2, ref default_table_behavior, ref auto_fit_behavior); // Compose the headers. string name1 = zone1.DisplayName; name1 = name1.Replace(") ", ")\n"); name1 = name1.Replace(" (", "\n("); if (name1.EndsWith("\n")) name1 = name1.Substring(0, name1.Length - 1); string name2 = zone2.DisplayName; name2 = name2.Replace(") ", ")\n"); name2 = name2.Replace(" (", "\n("); if (name2.EndsWith("\n")) name2 = name2.Substring(0, name2.Length - 2); word_table.Cell(1, 1).Range.Text = name1; word_table.Cell(1, 2).Range.Text = name2; // Draw the hour conversions. DateTime time1 = new DateTime( DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0); for (int hour = 1; hour <= 24; hour++) { word_table.Cell(hour + 1, 1).Range.Text = time1.ToShortTimeString(); DateTime time2 = TimeZoneInfo.ConvertTime(time1, zone1, zone2); string text2 = time2.ToShortTimeString(); if (time1.Date < time2.Date) text2 += " (tomorrow)"; else if (time1.Date > time2.Date) text2 += " (yesterday)"; word_table.Cell(hour + 1, 2).Range.Text = text2; time1 = time1.AddHours(1); } // Center the table's columns. word_table.Range.Font.Size = 10; word_table.Range.Font.Name = "Times New Roman"; word_table.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; word_table.Range.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceExactly; word_table.Range.ParagraphFormat.LineSpacing = 12f; word_table.Range.ParagraphFormat.SpaceAfter = 0f; // Save the document. string filename = Application.StartupPath + "\\TimeConversion.docx"; object filename_obj = filename; word_doc.SaveAs(ref filename_obj, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); // Close down Word. object save_changes = false; word_doc.Close(ref save_changes, ref missing, ref missing); word_app.Quit(ref save_changes, ref missing, ref missing); MessageBox.Show("Done"); }

The program starts by converting your timezone selections from the ComboBoxes to TimeZoneInfo objects.

Next the program creates a Word application and sets its Visible property to true. (Don't do that if you don't want the Word document to be visible.)

The program uses the Word application's Documents.Add method to create a new Word document. It gets a Range object representing the start of the document. It then uses the document's Tables.Add method to create a new table with 25 rows and 2 columns.

Next the program composes the column headers by inserting carriage returns between parenthesized parts of the timezone names. For example, it converts "(GMT-07:00) Mountain Time (US & Canada)" into:

(GMT-07:00)
Mountain Time
(US & Canada)

After building the column headers, the program uses the table's methods to set the text in the column headers.

Next the program generates the timezone conversion data. The program uses the year, month, and day of the date you selected to create a DateTime value. It then loops over the hours of the day 1 through 24. For each hour, the program writes the first timezone's time into the table.

Next the program uses the TimeZoneInfo class's ConvertTime method to convert that time to the other timezone and creates a string representation. If the new time has a day different from the first time, the program adds "(tomorrow)" or "(yesterday)" to the string. It then adds the string to the table.

After it has finished filling the table, the program sets the font size, font name, and paragraph spacing for the text in the table.

The code finishes by saving the Word document and closing down Word.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.