'Visual Studio C# Get sheet visible property
I'm trying to fill a dictionary with name as key, and an array with codename and if sheet is visible. This is where I ran into a conversion issue with sheet.visible
If I use == true I get this error: if I use == Excel.XlSheetVisibility.xlSheetVisible like in the official Microsoft documentation, I get this error:
using Microsoft.Office.Tools.Ribbon;
using System;
using Excel = Microsoft.Office.Interop.Excel;
using Forms = System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Text;
private Dictionary<string, string[]> getSheetsDict()
{
Excel.Workbook wb = Globals.ThisAddIn.Application.ActiveWorkbook;
Dictionary<string, string[]> dSheets = new Dictionary<string, string[]>();
if (wb.Sheets.Count > 0)
{
foreach (Excel.Worksheet sheet in wb.Sheets)
{
//string[] sDetails = { sheet.CodeName, Convert.ToString(sheet.Visible) };
string[] sDetails = { sheet.CodeName, (sheet.Visible == true) }; //Error 1
string[] sDetails = { sheet.CodeName, (sheet.Visible == Excel.XlSheetVisibility.xlSheetVisible) }; //Error 2
dSheets.Add(sheet.Name, sDetails);
}
}
return dSheets;
}
Solution 1:[1]
Sorry I missed this, I come from VBA where boolean can be implicitly converted to String. I defined the array as an array of Objects instead of Strings. Reverberated the change through my code and not it's running smoothly.
Object[] sDetails = { sheet.CodeName, sheet.Visible };
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dumitru Daniel |