ComplexFields.vb
C# VB
'''' This code is part of Document Solutions for Word demos.'' Copyright (c) MESCIUS inc. All rights reserved.''Imports System.Collections.GenericImports System.LinqImports GrapeCity.Documents.Word
'' This sample demonstrates the use of complext fields.'' It adds a complex field that tests whether the current date'' Is the New Year day, And modifies the text in the document'' accordingly. See also DateAndTime sample.Public Class ComplexFields Function CreateDocx() As GcWordDocument Dim doc = New GcWordDocument()
'' Add a title doc.Body.Paragraphs.Add("Testing whether it is the New Year yet").Style = doc.Styles(BuiltInStyleId.Title)
'' Add a paragraph And get its range: Dim rng = doc.Body.Paragraphs.Add().GetRange() '' Add a static text: rng.Runs.Add("Today is ")
'' Add a complex field with "IF" instruction. We also provide '' a pre-calculated value for the sake of PDF export, as DsWord '' does Not yet support field calculation - see DateAndTime. '' Note also that because the code field will Not be calculated '' in the PDF export, the "NOT " won't be bold in it: Dim val As String If Util.TimeNow().DayOfYear = 1 Then val = "" Else val = "NOT " End If Dim f = rng.ComplexFields.Add("IF ", val) '' Add a complex field with "DATE" instruction: f.GetCodeRange().ComplexFields.Add(" DATE \@ ""M-d"" ") '' Add additional instruction to the "IF" field to compare the nested '' DATE field result with "1-1" And return "NOT " if it Is true, '' also make the "NOT " bold if visible: f.CodeFields.Add("<> ""1-1"" ""Not """).ParentRun.Font.Bold = True
'' Add a static text rng.Runs.Add("New Year's Day.")
'' Done Return doc End FunctionEnd Class