DateAndTime.vb
  1. ''
  2. '' This code is part of Document Solutions for Word demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.Drawing
  6. Imports System.Globalization
  7. Imports GrapeCity.Documents.Word
  8.  
  9. '' This sample shows how to add simple date and time fields to a Word document.
  10. '' Note that DsWord does not yet support field calculation. But fields' values
  11. '' calculated in code can be supplied for a field, as this sample demonstrates.
  12. Public Class DateAndTime
  13. Function CreateDocx() As GcWordDocument
  14. Dim now = Util.TimeNow()
  15.  
  16. Dim doc = New GcWordDocument()
  17.  
  18. Dim p0 = doc.Body.Paragraphs.Add("DATE field with default formatting: ")
  19. p0.GetRange().SimpleFields.Add("DATE", now.ToString("d", CultureInfo.GetCultureInfo("en-US")))
  20. p0.GetRange().Runs.Add(vbCrLf + "TIME field with default formatting: ")
  21. p0.GetRange().SimpleFields.Add("TIME", now.ToString("t", CultureInfo.GetCultureInfo("en-US")))
  22.  
  23. Dim p1 = doc.Body.Paragraphs.Add(
  24. "The following tables demonstrates some custom date/time formats. " +
  25. "The left column shows the field code, right column contains the actual field using that code.")
  26.  
  27. '' Add and setup the table to contain the samples:
  28. Dim t = doc.Body.Tables.Add(0, 0)
  29. t.Style = doc.Styles.Add("Table style 1", StyleType.Table)
  30. For Each border In t.Style.Table.Borders
  31. border.LineStyle = LineStyle.Single
  32. border.LineWidth = 0.5F
  33. border.Color.RGB = Color.Black
  34. Next
  35.  
  36. '' Sample DATE formats:
  37. Dim sampleDateFormats As String() =
  38. {
  39. "DATE \@ ""M/d/yyyy""",
  40. "DATE \@ ""dddd, MMMM dd, yyyy""",
  41. "DATE \@ ""MMMM d, yyyy""",
  42. "DATE \@ ""M/d/yy""",
  43. "DATE \@ ""yyyy-MM-dd""",
  44. "DATE \@ ""d-MMM-yy""",
  45. "DATE \@ ""M.d.yyyy""",
  46. "DATE \@ ""MMM. d, yy""",
  47. "DATE \@ ""d MMMM yyyy""",
  48. "DATE \@ ""MMMM yy""",
  49. "DATE \@ ""MMM-yy""",
  50. "DATE \@ ""M/d/yyyy h:mm am/pm""",
  51. "DATE \@ ""M/d/yyyy h:mm:ss am/pm""",
  52. "DATE \@ ""h:mm am/pm""",
  53. "DATE \@ ""h:mm:ss am/pm""",
  54. "DATE \@ ""HH:mm""",
  55. "DATE \@ ""'Today is 'MMMM d, yyyy"""
  56. }
  57.  
  58. '' Add sample format strings and corresponding fields:
  59. For Each fmt In sampleDateFormats
  60. Dim r = t.Rows.Add(New String() {fmt})
  61.  
  62. '' DsWord does not yet support field calculation, but it does allow you
  63. '' to provide values calculated in code, so that's what we do here.
  64. '' We use the fact that most date/time format strings are the same
  65. '' in Word And .NET, but do replace Word's 'am/pm' with .NET 'tt':
  66. Dim f = fmt.Substring("DATE \@ ".Length).Trim("""").Replace("am/pm", "tt")
  67. r.Cells.Add().GetRange().Paragraphs.First.GetRange().SimpleFields.Add(fmt, now.ToString(f, CultureInfo.GetCultureInfo("en-US")))
  68. Next
  69.  
  70. '' Done:
  71. Return doc
  72. End Function
  73. End Class
  74.