3

I'm trying to access an item on the Styles collection of a word document, I was wondering if there's a way to know if an item with a given key exists without it throwing an exception.

Right now I'm doing something like:

newStyle = _document.Styles.get_Item(ref styleName);

This works when the item exists, if it doesn't it will fail (I know that I can use a try/catch block but I would prefer not to.

Thanks for your help

willvv
  • 8,439
  • 16
  • 66
  • 101
  • Never use 2 dots with COM objects: http://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects-in-c/158752#158752 – Mark Byers Jan 22 '10 at 21:47
  • Are you able to loop through the Styles collection? If so, that might be the only way – Chris Haas Jan 22 '10 at 21:50

1 Answers1

1

You need to loop over the styles and check the name, and exit the loop if you have a match.

Mikael Svenson
  • 39,181
  • 7
  • 73
  • 79