Excel Interop: Move a Worksheet

Howdy! I spent an hour or so trying to figure this one out. Hope this helps someone.

I am using Excel Interop with C#, and have a method that calls the .Move(Before, After) function on a Worksheet. I was trying to move a sheet to the end, and was using this code:

mySheet.Move(null, myWorkbook.Worksheets[myWorkbook.Worksheets.Count])

I was getting an error that said “Unable to get Move property of Worksheet class”. After much searching and an hour of fooling around, I figured out the fix. You cannot use “null”. You have to use one of:

Solution 1: Use ‘System.Type.Missing’

myWorkbook.Worksheets[myWorkbook.Worksheets.Count] )

Solution 2: Use ‘After:’

myWorkbook.Worksheets[myWorkbook.Worksheets.Count] )

Either of those will get you there. For some reason, I was thinking I didn’t have to use the magical System.Type.Missing, but you do.



, ,

Comments are closed.

Powered by WordPress. Designed by Woo Themes