Highlight Cells without Application scope

Is there a way to highlight specific cells in an excel spreadsheet using ONLY workbook activities?

I am only finding ways that require the use of application scope.

Hi @kasey.betts ,
You can invoke the vb.net code to set color for cell like below.
Here is the code

    Dim excel As Microsoft.Office.Interop.Excel.Application
    Dim wb As Microsoft.Office.Interop.Excel.Workbook
    Dim ws As Microsoft.Office.Interop.Excel.Worksheet
    excel = New Microsoft.Office.Interop.Excel.Application

    wb = excel.Workbooks.Open("C:\Input\inputdata.xlsx", [ReadOnly]:=False)
    excel.Visible = True

    ws = CType(wb.Sheets("Summary"), Microsoft.Office.Interop.Excel.Worksheet)
    ws.Activate() 
  ws.Range("A2:A6").Interior.Color = RGB(255, 255, 255)
 wb.Save()
    wb.Close()
    excel.Quit()

Regards,

are there any arguments to import?

Are there any required dependencies beside the default?

You can change filename, sheetname, cell index as arguments to import
eg: strFilename, strSheetName, etc
regards,

I am getting the following errors.

image

I upgraded all my packages and am now only getting this one

image

ColorTranslator.ToOle(Color.Red)
You can try it with your color
my output


my code

Dim excel As Microsoft.Office.Interop.Excel.Application
Dim wb As Microsoft.Office.Interop.Excel.Workbook
Dim ws As Microsoft.Office.Interop.Excel.Worksheet
excel = New Microsoft.Office.Interop.Excel.Application
wb = excel.Workbooks.Open("D:\UiPath\Forum\Input\input.xlsx", [ReadOnly]:=False)
excel.Visible = True
ws = CType(wb.Sheets("Sheet1"), Microsoft.Office.Interop.Excel.Worksheet)
ws.Activate() 
ws.Range("A2:A6").Interior.Color = ColorTranslator.ToOle(Color.Red)
wb.Save()
wb.Close()
excel.Quit()

My code
You can try it
I added arguments to import
Highlight_Cells_without_Application_scope.zip (284.9 KB)
image


Regards,

Will this code work if Excel is NOT installed on the machine bot is running from?

I have it working on my person station (which has excel). but on the bot machine it gives me this error

Invoke code: Exception has been thrown by the target of an invocation.

I think yes
Have you edited the arguments on that machine yet?
or you can change to
ws.Range(in_Range).Interior.ColorIndex = 40

@kasey.betts

Interop cannot be used without excel being installed

cheers

Hi @kasey.betts

You can use the below vb.net code to apply color to the specific cells by using invoke code activity.

Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop.Excel

Public Class HighlightCells
    Public Sub HighlightExcelCells(filePath As String, sheetName As String, cellAddress As String)
        ' Create an Excel application
        Dim excelApp As New ApplicationClass()

        ' Make Excel visible (for testing purposes)
        excelApp.Visible = True

        ' Open the workbook
        Dim workbook As Workbook = excelApp.Workbooks.Open(filePath)

        ' Get the specified worksheet
        Dim worksheet As Worksheet = DirectCast(workbook.Sheets(sheetName), Worksheet)

        ' Get the range of the specified cell
        Dim cellRange As Range = worksheet.Range(cellAddress)

        ' Set the interior color of the cell to yellow (you can modify this as needed)
        cellRange.Interior.Color = RGB(255, 255, 0) ' Yellow color

        ' Save and close the workbook
        workbook.Save()
        workbook.Close()

        ' Quit Excel application
        excelApp.Quit()

        ' Release COM objects to avoid memory leaks
        Marshal.ReleaseComObject(cellRange)
        Marshal.ReleaseComObject(worksheet)
        Marshal.ReleaseComObject(workbook)
        Marshal.ReleaseComObject(excelApp)
    End Sub
End Class

Hope it helps!!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.