Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
6 out of 11 rated this helpful - Rate this topic

Understanding Optional Parameters in COM Interop

Many of the methods in Microsoft Office Word 2003 and Microsoft Office Excel 2003 accept optional parameters. Optional parameters are only optional when you call a Word or Excel method using Microsoft Visual Basic. When you use C# to program against Word or Excel, you must pass a value for all optional parameters.

Optional Parameters in Excel and Word

If you use Visual Basic to program against Word or Excel, you do not have to pass a value for each optional parameter 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 each parameter. Microsoft Visual Studio 2005 Tools for the Microsoft Office System projects include a global variable named missing that is assigned System.Type.Missing in the generated code. You can pass this global variable to assume the default value of each optional parameter. For example, the CheckSpelling method takes the following optional parameters:

  • 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 follows:

Globals.Sheet1.CheckSpelling(missing, missing, missing, missing);

Calling Word methods from C#, in general, is more complex than calling Excel methods, because you must pass all the optional parameters by reference.

For example, the 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

  • CustomDictionary2CustomDictionary10. 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 by writing code similar to the following in the ThisDocument class:

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);

Using the Global missing Variable

You can only pass the global missing variable for parameters that accept reference types. For value-type parameters, you must determine the actual default value, and pass that instead. For example, the Sort method 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.

See Also

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.