Hello,
(Disclaimer: I am new to VB .Net)
I am trying to create an application that automates a spreadsheet (creates worksheets, populates and formats). I want to center the column heading text.
This seems to work:
xlsheet.range("1:1").HorizontalAlignment = -4108
Why does this work (using -4108) I suspect this is not the obvious way to do this.

HorizontalAlignment
gtsamoht
Hi,
The code that you wrote works because it is valid syntax, but it isn't a good way to code. Your question illustrates the problem; I can't tell what this code does by reading it because I have no idea what -4108 is supposed to mean.
A better way to write this code would be to replace -4108 with symbol that has meaning to humans but that the compiler will still evaluate as -4108. As it turns out, Excel has already defined a symbolic constant to represent this value: xlCenter.
So a better way to write that code would be something like:
xlsheet.range("1:1").HorizontalAlignment = Constants.xlCenter
Note that the example above assumes that you have the Excel namespace imported.
I'm not sure how you came up with the definition for xlCenter (-4108), but the help topic for the property hints at its existence. In the example, they use xlLeft. With that information, the approach I would take would be to go to the View menu, choose Object Browser, and then type xlLeft in the search combo box. That would have shown me that xlLeft was a member of Microsoft.Office.Interop.Excel.Constants. Clicking on "Constants" enables me to browse the class and I would have found all of the constants available to me. Since they are pretty self explanatory, I would have seen xlCenter and understood that was what I needed to use to get the HorizontalAlignment property to align center.
Sincerely,
Geoff Darst
Microsoft VSTO Team
Robert_sun
Thanks Geoff,
I agree, not a good way to code. That was my problem, I didn't know where to look up the available options in the object browser (under Microsoft.Office.Interop.Excel.Constants).
I got the -4108 (and the values for xlLeft and xlRight) by assigning the value of HorizontalAlignment to a variable (after altering it in the worksheet), then displaying that variable.