Use the below code to get to get the first the s=first sheet and rename that sheet:
Dim app As Microsoft.Office.Interop.Excel.Application
Dim wb As Microsoft.Office.Interop.Excel.Workbook
Dim firstSheet As Microsoft.Office.Interop.Excel.Worksheet
app = New Microsoft.Office.Interop.Excel.Application()
app.Visible = False
app.AskToUpdateLinks = False
wb = app.Workbooks.Open(ExcelFilePath, False, False)
If wb.Sheets.Count > 0 Then
firstSheet = CType(wb.Sheets(1), Microsoft.Office.Interop.Excel.Worksheet)
sheetName = firstSheet.Name
' Rename the first sheet
firstSheet.Name = "type your required NewSheet Name" ' Replace "New Sheet Name" with your desired name
Else
' Handle the case where there are no sheets in the workbook
' You can set a default value or raise an error as needed.
sheetName = "No Sheets Found"
End If
wb.Save()
wb.Close()
app.Quit()
'System.Runtime.InteropServices.Marshal.ReleaseComObject(wb)
'System.Runtime.InteropServices.Marshal.ReleaseComObject(app)
In ExcelFilePath argument pass the excel file path. Create a variable sheetName in the process and pass that in sheetName argument you can get the sheetname out in a variable you can use the new sheet name in the next below workflow i.e for example Read Range Workbook.
Or
Since you have done half of the process the below link will help you complete the rest too
To rename an Excel sheet in UiPath, you can use the Invoke VBA activity within an Excel Application Scope. Here’s how you can do it:
Excel Application Scope*: Use this activity to open the Excel file where you want to rename the sheet. Set the WorkbookPath property to your “QuotePath” variable.
Invoke VBA*: Place this activity inside the Excel Application Scope.
Write a VBA script to rename the sheet. Here is an example of a VBA function that renames a sheet:
vbaCopy code
Public Sub RenameSheet(oldName As String, newName As String)
Sheets(oldName).Name = newName
End Sub
Save the VBA script in a .txt file on your machine.
Invoke VBA Properties*:
Set the CodeFilePath property to the path of the .txt file containing your VBA code.
Set the EntryMethodName property to the name of the subroutine in your VBA code, e.g., “RenameSheet”.
In the EntryMethodParameters, pass an array with the old and new sheet names, e.g., {"oldSheetName", "NewSheetName"}.
Execution*: Run the workflow. The Invoke VBA activity will execute the VBA script and rename the sheet accordingly.
Here’s an example of how to configure the Invoke VBA activity within UiPath:
Hi @ppr, thank you for the response. I tried this before creating the topic. Sadly, I can’t find a way to use this solution in the background, so it doesn’t meet my needs.
Hey @postwick, thank you for your approach. Unfortunately, it is giving me this error:
Did you create the necessary arguments? Also, it shouldn’t be inside Excel Application Scope. Maybe that’s what is causing the error. Your Excel Application Scope has hold of the file and the Invoke Code is erroring when trying to access the file.