Home > Mobile >  C# Excel Dialog color palette
C# Excel Dialog color palette

Time:07-19

how can I address an Excel dialog for the color palette via C# so that the user can select a cell color, for example?

In VBA this could be done with the following code:

If Application.Dialogs(84).Show <> False Then
        varColor = .Cells(3, enuFormatting.CellColor).Interior.Color
        'Convert Color to RGB
        modul.Color_RGB varColor, intRed, intGreen, intBlue
        'Preview
        Me.lbl_FormatFont.BackColor = RGB(intRed, intGreen, intBlue)
    End If

 Public Sub Color_RGB(ByVal varColor As Variant, ByRef intRed As Integer, ByRef intGreen As Integer, ByRef intBlue As Integer)
    'Convert color index to RGB
    On Error Resume Next
    intRed = varColor Mod 256
    varColor = (varColor - intRed) / 256
    intGreen = varColor Mod 256
    varColor = (varColor - intGreen) / 256
    intBlue = varColor Mod 256
End Sub

Greetings

CodePudding user response:

You may get the collection of all the Dialog objects in Microsoft Excel:

Application excel = new Microsoft.Office.Interop.Excel.Application();
...
Dialogs dialogs = excel.Dialogs;

and then you get the xlDialogPatterns and invoke the Show() method:

dialogs.Item[XlBuiltInDialog.xlDialogPatterns].Show();

CodePudding user response:

The following code worked for me:

using Excel = Microsoft.Office.Interop.Excel;
namespace YourNamespace
{
public static class YourClass
{
Excel.Anwendung excelApp;
Excel.Arbeitsmappe excelBook;
Excel.Arbeitsblatt excelSheet;

excelApp = new Excel.Application();
Excel.Workbook excelBook;

excelApp = new Excel.Application();
excelApp.Visible = true;
excelBook = excelApp.Workbooks.Open(GetProjectPath()   "YourFileName.xlsx"); 
excelSheet = excelBook.Sheets["YourSheetName"];
excelApp.Dialogs.Item[Excel.XlBuiltInDialog.xlDialogPatterns].Show();
}
}
  • Related