The Variable missing and Optional Parameters in Office Solutions
Many of the methods in the object models of Microsoft Office applications accept optional parameters. Optional parameters are only optional when you call a method using Visual Basic. When you use C# to program against a Microsoft Office application, you must pass a value for all parameters.
Optional Parameter Differences in Visual Basic and Visual C#
If you use Visual Basic to program against a Microsoft Office application, you do not have to pass a value for optional parameters because the default values are automatically used for each missing parameter. However, optional parameters are not supported in C#, so you must pass a value to the method for every parameter.
Visual Studio Tools for Office projects include a global variable named missing that is assigned to the value Type.Missing in the generated code. You can pass this global variable to assume the default value of each optional parameter that accepts a reference type, such as Object. For optional parameters that accept a value type, you must pass the actual default value. For more information, see Passing Value Types to Optional Parameters in Visual C#.
Example in Excel
The Worksheet.CheckSpelling method takes the following optional parameters that accept reference types:
CustomDictionary. Indicates whether to use a custom dictionary if the word is not found in the main dictionary.
IgnoreUppercase. Indicates whether to ignore uppercase.
AlwaysSuggest. Indicates whether to have Excel display a list of suggested alternate spellings when an incorrect spelling is found.
SpellLang. Indicates the language of the dictionary being used.
You can accept the default value of these parameters as shown in the following code example.
Globals.Sheet1.CheckSpelling()
Globals.Sheet1.CheckSpelling(missing, missing, missing, missing);
Example in Word
Calling Microsoft Office Word methods from C#, in general, is more complex than calling methods for other Office applications, because you must pass all the optional parameters by reference.
For example, the Document.CheckSpelling method takes the following optional parameters for spellchecking a Word document:
CustomDictionary. The file name of the custom dictionary.
IgnoreUppercase. Indicates whether to ignore uppercase.
AlwaysSuggest. Indicates whether to have Word display a list of suggested alternate spellings when an incorrect spelling is found
CustomDictionary2 – CustomDictionary10. The file names of additional custom dictionaries. You can specify as many as nine additional dictionaries.
You can accept the default value of these parameters as shown in the following code example.
Me.CheckSpelling()
this.CheckSpelling(
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
Passing Value Types to Optional Parameters in Visual C#
In Visual C#, you can only pass the global missing variable for optional parameters that accept reference types. For optional parameters that accept value types, you must determine the actual default value, and pass that instead.
For example, the Sort method of the NamedRange class accepts a large number of enumerated values as parameters, such as XlSortOrder. Because this is a value type, you must specify one of the XlSortOrder values rather than passing the global missing variable.
For more information about value and reference type parameters, see Passing Arguments by Value and by Reference (for Visual Basic) and Passing Parameters (C# Programming Guide).