<p>Many of the parameters for interacting with the Office Object model in VSTO require object parameters that are passed by reference, even when the notional type of the parameter is an int or string.</p>

<ol>
<li>I suppose that this mechanism is used so that code can modify the parameter, although I can't figure out why these need to be passed as generic object instead of as their more appropriate types.  Can anyone enlighten me?</li>
<li><p>The mechanism I've been using (cribbed from help and MSDN resources) essentially creates a generic object that contains the appropriate data and then passes that to the method, for example:</p>

<p>object nextBookmarkName = "NextContent";
object nextBookmark = this.Bookmarks.get_Item( ref nextBookmarkName ).Range;</p>

<p>Microsoft.Office.Interop.Word.Range newRng = this.Range( ref nextBookmark, ref nextBookmark );</p></li>
</ol>

<p>This seems like a lot of extra code, but I can't see a better way to do it.  I'm sure I'm missing something; what is it?  Or is this really the best practice?</p>

[{'answer_id': 73310, 'author': 'KTamas', 'author_id': 6541, 'author_profile': 'https://Stackoverflow.com/users/6541', 'pm_score': 0, 'selected': False, 'text': "<p>I'd be interested in this too. I'm coding several apps that uses automation in Word and I even have things like</p>\n\n<pre><code>object oFalse = false, oTrue = true, oOne = 1;\n</code></pre>\n\n<p>It's very nasty, but it's the only way I know so far. </p>\n\n<p>The only thing I can think of is writing a wrapper class for the frequently used functions...</p>\n"}, {'answer_id': 74950, 'author': 'Joe', 'author_id': 13087, 'author_profile': 'https://Stackoverflow.com/users/13087', 'pm_score': 1, 'selected': True, 'text': '<p>I think it was just poor design of the original Word object model.  I know that passing strings by reference can be slightly faster in the COM world because it avoids the need to make a copy, so perhaps that was part of the justification.  But the downside is that the callee can modify the value, and in most cases with Word they are input parameters.</p>\n\n<p>I think your technique is the best practice.  For the millions of optional parameters that many of the Word object model methods require, you can create a single static field "missing" something like: </p>\n\n<p>object missing = Type.Missing;</p>\n\n<p>// Example\nobject fileName = ...\ndocument.SaveAs(ref fileName, ref missing, ref missing, ref missing,\n                ref missing, ref missing, ref missing, ref missing, ref missing,\n                ref missing, ref missing, ref missing, ref missing, ref missing,\n                ref missing, ref missing);</p>\n'}, {'answer_id': 165598, 'author': 'rasx', 'author_id': 22944, 'author_profile': 'https://Stackoverflow.com/users/22944', 'pm_score': 2, 'selected': False, 'text': '<p>I agree with Joe. I even developed helper structs and classes like this one:</p>\n\n<pre><code>internal struct Argument\n{\n    internal static object False = false;\n\n    internal static object Missing = System.Type.Missing;\n\n    internal static object True = true;\n}\n</code></pre>\n\n<p>And this one:</p>\n\n<pre><code>/// &lt;summary&gt;\n/// Defines the "special characters"\n/// in Microsoft Word that VSTO 1.x\n/// translates into C# strings.\n/// &lt;/summary&gt;\ninternal struct Characters\n{\n    /// &lt;summary&gt;\n    /// Word Table end-of-cell marker.\n    /// &lt;/summary&gt;\n    /// &lt;remarks&gt;\n    /// Word Table end-of-row markers are also\n    /// equal to this value.\n    /// &lt;/remarks&gt;\n    internal static string CellBreak = "\\r\\a";\n\n    /// &lt;summary&gt;\n    /// Word line break (^l).\n    /// &lt;/summary&gt;\n    internal static string LineBreak = "\\v";\n\n    /// &lt;summary&gt;\n    /// Word Paragraph break (^p).\n    /// &lt;/summary&gt;\n    internal static string ParagraphBreak = "\\r";\n}\n</code></pre>\n\n<p>And a few more...</p>\n'}, {'answer_id': 256665, 'author': 'shahkalpesh', 'author_id': 23574, 'author_profile': 'https://Stackoverflow.com/users/23574', 'pm_score': 0, 'selected': False, 'text': '<p>I think, all of this is taken care of with VS.NET 2010 and new language constructs introduced in c# 4.0 (c# will have optional arguments).</p>\n\n<p>See the video by Anders Hejlberg at PDC 2008 on channel9 for changes related to office development.</p>\n\n<p>I cant find that link but this could also be helpful.<br>\n<a href="http://channel9.msdn.com/shows/Going+Deep/Inside-C-40-dynamic-type-optional-parameters-more-COM-friendly/" rel="nofollow noreferrer">http://channel9.msdn.com/shows/Going+Deep/Inside-C-40-dynamic-type-optional-parameters-more-COM-friendly/</a></p>\n'}]
['https://Stackoverflow.com/questions/73261', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/8151/']